Warning: preg_replace(): Unknown modifier 'g'

PhpRegex

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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

Solution 3 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFoo LingView Question on Stackoverflow
Solution 1 - PhpridView Answer on Stackoverflow
Solution 2 - Phpp.s.w.gView Answer on Stackoverflow
Solution 3 - PhpCparelloView Answer on Stackoverflow