本文介绍了未知修饰符 'g' PHP 正则表达式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的模式是:/(productimages\/)(\w*)(\/v\/)(\w*)(.jpg)/g
和数据:http://gskinner.com/RegExr/?2ujor
和php代码:
$regexp = '/(productimages\/)(\w*)(\/v\/)(\w*)(.jpg)/g';if(preg_match("$regexp", $input, $matches, PREG_SET_ORDER)) {for($i=0;$i<14;$i++){echo '--->'.$i.'-->'.$matches[0][$i];}}
结果:警告:preg_match() [function.preg-match]:未知修饰符'g'
$regexp = '/(productimages\/)(\w*)(\/v\/)(\w*)(.jpg)/g';if(preg_match_all("$regexp", $input, $matches, PREG_SET_ORDER)) {for($i=0;$i<14;$i++){echo '--->'.$i.'-->'.$matches[0][$i];}}
结果:警告:preg_match_all() [function.preg-match-all]:未知修饰符'g'
此解决方案无效!:|未知修饰符 'g' in...";在 PHP 中使用 preg_match 时?
我该怎么办?
解决方案
切换到 preg_match_all
是正确的,现在您需要做的就是从正则表达式中删除g":
$regexp = '/(productimages\/)(\w*)(\/v\/)(\w*)(.jpg)/';
my pattern is: /(productimages\/)(\w*)(\/v\/)(\w*)(.jpg)/g
and data: http://gskinner.com/RegExr/?2ujor
and php code:
$regexp = ' /(productimages\/)(\w*)(\/v\/)(\w*)(.jpg)/g';
if(preg_match("$regexp", $input, $matches, PREG_SET_ORDER)) {
for($i=0;$i<14;$i++){
echo '--->'.$i.'-->'.$matches[0][$i];
}}
$regexp = ' /(productimages\/)(\w*)(\/v\/)(\w*)(.jpg)/g';
if(preg_match_all("$regexp", $input, $matches, PREG_SET_ORDER)) {
for($i=0;$i<14;$i++){
echo '--->'.$i.'-->'.$matches[0][$i];
}}
this solution not worked! :|"Unknown modifier 'g' in..." when using preg_match in PHP?
what should i do?
解决方案
Switching to preg_match_all
was correct, now all you need to do is remove the 'g' from your regex:
$regexp = '/(productimages\/)(\w*)(\/v\/)(\w*)(.jpg)/';
这篇关于未知修饰符 'g' PHP 正则表达式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!