php 警告:preg_replace():未知修饰符“g”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19061037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:44:41  来源:igfitidea点击:

Warning: preg_replace(): Unknown modifier 'g'

phpregex

提问by Foo Ling

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 ....

警告:preg_replace(): 未知修饰符“g” in ....

Why?

为什么?

回答by rid

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

g是隐含的preg_replace()。您不需要包含它。

回答by p.s.w.g

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) 用于指定要进行的替换次数:

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

limit每个主题字符串中每个模式的最大可能替换次数。默认为-1(无限制)。

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

回答by Cparello

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

/您要替换的字符串中的字母 G 前有一个。