我一直在研究Youtube和Vimeo嵌入代码解析器,我正在尝试使用正则表达式来解决问题。
我发现了两种模式,它们正在使用 eregi(( 函数,但不幸的是不适用于 preg_match((。给出"分隔符不得为字母数字或反斜杠"错误。
如何将这些模式从 POSIX 转换为 PCRE?
对于优酷;
'/v'/(.{11})|'/embed'/(.{11})
对于维密欧;
player'.vimeo'.com'/video/([0-9]*)"
这是
针对youtube的:$pattern = '/'/v'/(.{11})|'/embed'/(.{11})/';
这就是Vimeo:$pattern = '/player'.vimeo'.com'/video'/([0-9]*)/';
使用 PCRE 时,请确保将表达式括在 /expression/
(斜杠(中,并在所有/
进行转义。我注意到你有时这样做,有时你没有...
我发现这个在我帮助开发的网站中很有帮助。感谢和功劳归于山脊行者。
// Linkify youtube URLs which are not already links.
function linkifyYouTubeURLs($text) {
$text = preg_replace('~
# Match non-linked youtube URL in the wild. (Rev:20111012)
https?:// # Required scheme. Either http or https.
(?:[0-9A-Z-]+'.)? # Optional subdomain.
(?: # Group host alternatives.
youtu'.be/ # Either youtu.be,
| youtube'.com # or youtube.com followed by
'S* # Allow anything up to VIDEO_ID,
[^'w'-'s] # but char before ID is non-ID char.
) # End host alternatives.
(['w'-]{11}) # $1: VIDEO_ID is exactly 11 chars.
(?=[^'w'-]|$) # Assert next char is non-ID or EOS.
(?! # Assert URL is not pre-linked.
[?=&+%'w]* # Allow URL (query) remainder.
(?: # Group pre-linked alternatives.
[''"][^<>]*> # Either inside a start tag,
| </a> # or inside <a> element text contents.
) # End recognized pre-linked alts.
) # End negative lookahead assertion.
[?=&+%'w-]* # Consume any URL (query) remainder.
~ix',
'<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>',
$text);
return $text;
}
您应该能够从那里剥离您需要的内容,并且它会处理所有样式的YouTube链接。Vimeo从那里开始应该不会太难。