警告: preg_place() : 未知修饰符‘ g’

这个正则表达式出错了。

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

错误

警告 : preg _ place () : ... 中的未知修饰符‘ g’。

为什么?

48069 次浏览

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

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

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