我正在浏览 stackoverflow 并在这里找到了一个很棒的正则表达式代码。可能有其他方法可以隔离 youtube 视频 ID,但我选择使用正则表达式进行学习。input1
带有(如下所示)的正则表达式代码忽略&
字符之后的所有内容。这会清除视频 id,因此会给出不正确或空的 id 结果。为什么正则表达式会在之后清除所有内容&
?
错误:
输入 1: http ://www.youtube.com/watch?feature&v= 317a815FLWQ
结果1:http//www.youtube.com/watch?feature
普通的:
输入2:http ://www.youtube.com/watch?v=spDj54kf-vY&feature=g-vrec
结果 2: http ://www.youtube.com/watch?v=spDj54kf-vY
正则表达式代码(带有原始评论)
$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;