【问题标题】:Unknown modifier 'g' PHP regex error未知修饰符 'g' PHP 正则表达式错误
【发布时间】:2011-11-09 16:36:16
【问题描述】:

我的模式是:/(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”

此解决方案无效! :| "Unknown modifier 'g' in..." when using preg_match in PHP?

我该怎么办?

【问题讨论】:

  • 与你的问题无关,但你应该使用\.来匹配一个点。

标签: php regex


【解决方案1】:

切换到preg_match_all 是正确的,现在您需要做的就是从您的正则表达式中删除“g”:

$regexp = '/(productimages\/)(\w*)(\/v\/)(\w*)(.jpg)/';

【讨论】:

  • 我想要所有匹配的字符串!删除 g 只返回第一个匹配的字符串 $matches[0][0]
【解决方案2】:

There's no g modifier.

删除那个 g 就可以了

$regexp = '/(productimages\/)(\w*)(\/v\/)(\w*)(.jpg)/';

【讨论】:

  • 没有 G 它只返回第一个匹配的字符串!
    --->0-->productimages/Routers/v/CISCO1941.jpg--->1-->productimages/--- >2-->路由器--->3-->/v/--->4-->CISCO1941--->5-->.jpg--->6-->--->7- ->--->8-->--->9-->--->10-->--->11-->--->12-->--->13-->
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-27
  • 2013-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多