Foo Ling
Foo Ling

Reputation: 1101

Warning: preg_replace(): Unknown modifier 'g'

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?

Upvotes: 104

Views: 54077

Answers (3)

p.s.w.g
p.s.w.g

Reputation: 149040

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

Upvotes: 56

rid
rid

Reputation: 63542

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

Upvotes: 207

Cparello
Cparello

Reputation: 1585

There is a / before the letter G in the string you're replacing.

Upvotes: -2

Related Questions