【问题标题】:regex - trying to auto embed youtube videos [duplicate]正则表达式 - 尝试自动嵌入 youtube 视频 [重复]
【发布时间】:2011-12-09 02:33:28
【问题描述】:

可能重复:
How to embed YouTube videos in PHP?

function youtube($string)
{
preg_match('#(v\/|watch\?v=)([\w\-]+)#', $string, $match);
  return preg_replace(
    '#((http://)?(www.)?youtube\.com/watch\?[=a-z0-9&_]+)#i',
    "<div align=\"center\"><iframe title=\"YouTube video player\" width=\"480\" height=\"390\" src=\"http://www.youtube.com/embed/$match[2]\" frameborder=\"0\" allowfullscreen></iframe></div>",
    $string);
}

这个函数几乎可以工作,但是由于“&”字符变成了“&”它并没有完全摆脱整个 URL。

http://www.youtube.com/watch?v=oRcUzu_xdJI&feature=feedu

例如返回

嵌入式视频&feature=feedu

我需要如何修改第二个正则表达式?

#((http://)?(www.)?youtube\.com/watch\?[=a-z0-9&_]+)#i

【问题讨论】:

标签: php regex


【解决方案1】:

如果您的问题只是 URL 字符串中的 &amp;amp;,则在您的第二个正则表达式中扩展字符类。它只需要也替换;

#((http://)?(www.)?youtube\.com/watch\?[=a-z0-9&_;]+)#i

不过,匹配\S+ 可能更有意义。该字符串中可能还有其他意想不到的参数,如果您的字符类过于严格,则会出现更多剩余参数。

【讨论】:

    猜你喜欢
    • 2018-03-18
    • 2013-03-19
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 2013-01-19
    • 2016-11-19
    相关资源
    最近更新 更多