正则表达式在字符串中查找 Youtube 链接 [英] Regex to find Youtube Link in string
本文介绍了正则表达式在字符串中查找 Youtube 链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的字符串:
I have a string like that:
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, https://www.youtube.com/watch?v=7TL02DA5MZM when an unknown printer took a galley of type and scrambled it to make a type
这就是我所拥有的:
preg_match("(?:http://)?(?:www.)?(?:youtube.com|youtu.be)/(?:watch\?)?([^\s]+?)", $content, $m);
var_dump( $m );
并希望从中提取 youtube 链接.视频 ID 也可以.
and want to extract the youtube link form it. The video id, would be okay, too.
感谢您的帮助!
推荐答案
这对你有用,
\S*\bwww\.youtube\.com\S*
\S*
匹配零个或多个非空格字符.
\S*
matches zero or more non-space characters.
代码是,
preg_match('~\S*\bwww\.youtube\.com\S*~', $str, $matches);
我对你原来的正则表达式做了一些更正.
And i made Some corrections to your original regex.
(?:https?://)?(?:www.)?(?:youtube.com|youtu.be)/(?:watch\?v=)?([^\s]+)
$str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, https://www.youtube.com/watch?v=7TL02DA5MZM when an unknown printer took a galley of type and scrambled it to make a type";
preg_match('~(?:https?://)?(?:www.)?(?:youtube.com|youtu.be)/(?:watch\?v=)?([^\s]+)~', $str, $match);
print_r($match);
输出:
Array
(
[0] => https://www.youtube.com/watch?v=7TL02DA5MZM
[1] => 7TL02DA5MZM
)
这篇关于正则表达式在字符串中查找 Youtube 链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文