preg_match()未知修饰符'['帮助 [英] preg_match() Unknown modifier '[' help
本文介绍了preg_match()未知修饰符'['帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个正则表达式来获取YouTube视频ID:
I have this regex for getting the YouTube video ID:
(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+
我从那里得到它:用于解析youtube yid的正则表达式
问题是我收到preg_match() Unknown modifier '['
警告.
我知道我必须封装正则表达式定界符,但是我不知道该怎么做.
I know I have to enclose the regex delimiters but I have no idea how to do this.
有帮助吗?
推荐答案
尝试以下操作:
<?php
$str = "http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I";
$pattern = '#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+#';
preg_match($pattern, $str, $matches);
print_r($matches);
?>
请注意,我在这里使用#
作为分隔符只是因为上面的正则表达式包含正斜杠,而转义它们会使表达式更难以阅读.这样可以清理几个像素.
Note, I'm using #
as a delimiter here simply because the regular expression above contains forward slashes and escaping them makes the expression more difficult to read. This cleans it up by just a few pixels.
这篇关于preg_match()未知修饰符'['帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文