转换YouTube链接直接iframe嵌入code [英] Convert youtube url to iframe embed code

查看:891
本文介绍了转换YouTube链接直接iframe嵌入code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图找到一个正则表达式替换用iframe嵌入code(C#)一个字符串的所有YouTube网址。显然,视频ID已提取。下面是应该匹配的URL模式:

所有可能的URL应改为:

 < IFRAME标题=YouTube视频播放的'width ='480'高度='390'SRC ='HTTP://www.youtube.com/embed/VIDEO_ID_EXTRACTEDFRAMEBORDER ='0'的allowFullScreen ='1'>&下; / iframe的>

可有人请点我一个正确的方向。

感谢您提前


解决方案

下面是正则表达式:

<$p$p><$c$c>(?:https?:\\/\\/)?(?:www\\.)?(?:(?:(?:youtube.com\\/watch\\?[^?]*v=|youtu.be\\/)([\\w\\-]+))(?:[^\\s?]+)?)

应该与你张贴的链接和提取的视频ID为 $ 1 。并用以下code更换了与&LT的联系; IFRAME /&GT;

 常量字符串输入=htt​​p://www.youtube.com/watch?v=bSiDLCf5u3s+
                     https://www.youtube.com/watch?v=bSiDLCf5u3s+
                     http://youtu.be/bSiDLCf5u3s+
                     www.youtube.com/watch?v=bSiDLCf5u3s+
                     youtu.be/bSiDLCf5u3s+
                     http://www.youtube.com/watch?feature=player_embedded&v=bSiDLCf5u3s+
                     www.youtube.com/watch?feature=player_embedded&v=bSiDLCf5u3s+
                     http://www.youtube.com/watch?v=_-QpUDvTdNY;
常量字符串模式= @\"(?:https?:\\/\\/)?(?:www\\.)?(?:(?:(?:youtube.com\\/watch\\?[^?]*v=|youtu.be\\/)([\\w\\-]+))(?:[^\\s?]+)?)\";
常量字符串替换=&LT; IFRAME标题=YouTube视频播放的'width ='480'高度='390'SRC ='HTTP://www.youtube.com/embed/$1FRAMEBORDER =0的allowFullScreen =' 1&GT;&LT; / IFRAME&gt;中;VAR RGX =新的正则表达式(模式);
VAR的结果= rgx.Replace(输入,更换);//结果==
//&LT; IFRAME标题=YouTube视频播放的'width ='480'高度='390'SRC ='HTTP://www.youtube.com/embed/bSiDLCf5u3sFRAMEBORDER =0的allowFullScreen ='1'&GT ;&LT; / IFRAME&GT;
// ...

I've been trying to find a regex pattern to replace all youtube URLs in a string with the iframe embed code (C#). Obviously the video ID has to extracted. Here is the url patterns that should match:

all possible urls should be replaced with:

<iframe title='YouTube video player' width='480' height='390' src='http://www.youtube.com/embed/VIDEO_ID_EXTRACTED' frameborder='0' allowfullscreen='1'></iframe>

Can someone please point me to a right direction.

Thank you in advance

解决方案

Here is the regex:

(?:https?:\/\/)?(?:www\.)?(?:(?:(?:youtube.com\/watch\?[^?]*v=|youtu.be\/)([\w\-]+))(?:[^\s?]+)?)

Should match all the links you posted and extracts the video ID as $1. And with the following code you replace the links with the <iframe/>:

const string input = "http://www.youtube.com/watch?v=bSiDLCf5u3s " +
                     "https://www.youtube.com/watch?v=bSiDLCf5u3s " +
                     "http://youtu.be/bSiDLCf5u3s " +
                     "www.youtube.com/watch?v=bSiDLCf5u3s " +
                     "youtu.be/bSiDLCf5u3s " +
                     "http://www.youtube.com/watch?feature=player_embedded&v=bSiDLCf5u3s " +
                     "www.youtube.com/watch?feature=player_embedded&v=bSiDLCf5u3s " +
                     "http://www.youtube.com/watch?v=_-QpUDvTdNY";
const string pattern = @"(?:https?:\/\/)?(?:www\.)?(?:(?:(?:youtube.com\/watch\?[^?]*v=|youtu.be\/)([\w\-]+))(?:[^\s?]+)?)";
const string replacement = "<iframe title='YouTube video player' width='480' height='390' src='http://www.youtube.com/embed/$1' frameborder='0' allowfullscreen='1'></iframe>";

var rgx = new Regex(pattern);
var result = rgx.Replace(input, replacement);

// result ==
// <iframe title='YouTube video player' width='480' height='390' src='http://www.youtube.com/embed/bSiDLCf5u3s' frameborder='0' allowfullscreen='1'></iframe>
// ...

这篇关于转换YouTube链接直接iframe嵌入code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆