Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: preg_replace(): Unknown modifier 'g'

Tags:

regex

php

I got an error by this regular expression...

$strTmp = preg_replace('~(<\/CharacterStyleRange>(.*?)\n*</CharacterStyleRange>)~gim ' , "</CharacterStyleRange>", $strTmp); 

Error

Warning: preg_replace(): Unknown modifier 'g' in ....

Why?

like image 884
Foo Ling Avatar asked Sep 27 '13 22:09

Foo Ling


2 Answers

g is implicit with preg_replace(). You don't need to include it.

like image 138
rid Avatar answered Nov 07 '22 15:11

rid


You don't have to specify the global flag. From the documentation, there is a separate parameter ($limit) used to specify the number of replacements to make:

limit The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).

So, unless you specify a positive number for this parameter, it will replace all occurrences by default:

$strTmp = preg_replace('~(<\/CharacterStyleRange>(.*?)\n*</CharacterStyleRange>)~im ' , "</CharacterStyleRange>", $strTmp); 
like image 44
p.s.w.g Avatar answered Nov 07 '22 17:11

p.s.w.g