我一直在研究 Youtube 和 Vimeo 嵌入代码解析器,我试图通过使用正则表达式来解决问题。
我发现了两种模式,它们正在使用 eregi() 函数,但不幸的是不适用于 preg_match()。给出“分隔符不能是字母数字或反斜杠”错误。
如何将这些模式从 POSIX 转换为 PCRE?
对于 Youtube;
\/v\/(.{11})|\/embed\/(.{11})
对于 Vimeo;
player\.vimeo\.com\/video/([0-9]*)"
我发现这个对我帮助开发的网站很有帮助。感谢并感谢ridgerunner。
// 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 从那里开始不应该太难。
这是给 youtube 的:$pattern = '/\/v\/(.{11})|\/embed\/(.{11})/';
那是给 Vimeo 的:$pattern = '/player\.vimeo\.com\/video\/([0-9]*)/';
使用 PCRE 时,请确保将表达式括在/expression/
(斜杠)中,并转义所有/
. 我注意到你有时会,有时你不会...