如何将4个扩展名与regexp匹配?
我累了这个:
$returnValue = preg_match('/(pdf|jpg|jpeg|tif)/', 'pdf', $matches);
Run Code Online (Sandbox Code Playgroud)
我不知道为什么我得到2场比赛?我是regexp中遗漏的东西吗?
array (
0 => 'pdf',
1 => 'pdf',
)
Run Code Online (Sandbox Code Playgroud)
I dont know why I get 2 matches
不,你只得到一场比赛.
$matches 有2个条目:
1st entry with index=0 是由你的正则表达式输入的整个匹配2nd entry with index=1 是第一个匹配的组,因为你的正则表达式括在括号中 如果您想避免2个条目,您可以使用non-capturing group:
$returnValue = preg_match('/(?:pdf|jpg|jpeg|tif)/', 'pdf', $matches);
Run Code Online (Sandbox Code Playgroud)
或者根本不对它们进行分组:
$returnValue = preg_match('/pdf|jpg|jpeg|tif/', 'pdf', $matches);
Run Code Online (Sandbox Code Playgroud)