Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown modifier 'g' PHP regex error

Tags:

regex

php

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?

like image 229
Amino Avatar asked Aug 02 '26 14:08

Amino


1 Answers

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)/';
like image 70
Paul Avatar answered Aug 04 '26 02:08

Paul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!