要嵌入的Youtube URL


Youtube URL to embed

我在stackoverflow上搜索并看到了许多解决方案,但没有一个对我有效。我有一个带有youtube URL 的字符串

<b>This is a youtube video http://www.youtube.com/watch?v=0xnKzTqrqrE and here is another one  http://www.youtube.com/watch?v=0xnKzTqsdds </b> I am not sure if it will work.

我希望所有youtube URL都被取代

<iframe src="http://youtube.com/embed/"> </iframe>

可能有不止一个视频url,我希望所有这些都能在不更改其余字符串的情况下进行转换。

提前感谢!!

在最简单的情况下,您可以替换:

http://www.youtube.com/watch?v=([a-zA-Z0-9]{11})

通过

<iframe width="420" height="315" src="//www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>

此处为php代码:

echo preg_replace('http://www.youtube.com/watch?v=([a-zA-Z0-9]{11})', '<iframe width="420" height="315" src="//www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>', $input);