Unknown modifier 'g' PHP regex error
Asked Answered
I

2

3

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];

}}

result: Warning: preg_match() [function.preg-match]: Unknown modifier '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];

}}

result: Warning: preg_match_all() [function.preg-match-all]: Unknown modifier 'g'

this solution not worked! :| "Unknown modifier 'g' in..." when using preg_match in PHP?

what should i do?

Itol answered 3/9, 2011 at 10:32 Comment(1)
Unrelated to your problem, but you should use \. to match a dot.Kinsler
K
8

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)/';
Klausenburg answered 3/9, 2011 at 10:35 Comment(1)
i want all matched strings! removing g returns only first matched string $matches[0][0]Itol
A
3

There's no g modifier.

Delete that g and it'll work

$regexp = '/(productimages\/)(\w*)(\/v\/)(\w*)(.jpg)/';
Anchusin answered 3/9, 2011 at 10:36 Comment(1)
without G it returns onlye first matched string!<br> --->0-->productimages/Routers/v/CISCO1941.jpg--->1-->productimages/--->2-->Routers--->3-->/v/--->4-->CISCO1941--->5-->.jpg--->6-->--->7-->--->8-->--->9-->--->10-->--->11-->--->12-->--->13-->Itol

© 2022 - 2024 — McMap. All rights reserved.