警告:preg_replace():未知修饰符']' [英] Warning: preg_replace(): Unknown modifier ']'
问题描述
我遇到以下错误:
警告:preg_replace():第38行上xxx.php中的未知修饰符']'
Warning: preg_replace(): Unknown modifier ']' in xxx.php on line 38
这是第38行的代码:
<?php echo str_replace("</ul></div>", "", preg_replace("<div[^>]*><ul[^>]*>", "", wp_nav_menu(array('theme_location' => 'nav', 'echo' => false)) )); ?>
如何解决此问题?
推荐答案
为什么会发生错误
在PHP中,正则表达式需要包含在一对 delimiters <中/a>.分隔符可以是任何非字母数字,非反斜杠,非空格字符; /
,#
,~
是最常用的.请注意,也可以使用括号样式的定界符,其中左括号和右括号是开始和结束定界符,即<pattern_goes_here>
,[pattern_goes_here]
等都有效.
Why the error occurs
In PHP, a regular expression needs to be enclosed within a pair of delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character; /
, #
, ~
are the most commonly used ones. Note that it is also possible to use bracket style delimiters where the opening and closing brackets are the starting and ending delimiter, i.e. <pattern_goes_here>
, [pattern_goes_here]
etc. are all valid.
在以下两种情况下通常会出现"未知修饰符X "错误:
The "Unknown modifier X" error usually occurs in the following two cases:
当您的正则表达式缺少分隔符时.
当您在内部使用定界符 而没有转义时.
在这种情况下,正则表达式为<div[^>]*><ul[^>]*>
. regex引擎将从<
到>
的所有内容都视为regex模式,然后将所有内容视为修饰符.
In this case, the regular expression is <div[^>]*><ul[^>]*>
. The regex engine considers everything from <
to >
as the regex pattern, and everything afterwards as modifiers.
Regex: <div[^> ]*><ul[^>]*>
│ │ │ │
└──┬──┘ └────┬─────┘
pattern modifiers
]
是未知的修饰符,因为它出现在结束的>
分隔符之后.这就是PHP抛出该错误的原因.
]
here is an unknown modifier, because it appears after the closing >
delimiter. Which is why PHP throws that error.
根据模式的不同,未知修饰词的抱怨也可能是关于*
,+
,p
,/
或)
或几乎任何其他字母/符号的.只有imsxeADSUXJu
是有效的PCRE修饰符.
Depending on the pattern, the unknown modifier complaint might as well have been about *
, +
, p
, /
or )
or almost any other letter/symbol. Only imsxeADSUXJu
are valid PCRE modifiers.
修复很容易.只需使用任何有效的定界符包装您的正则表达式模式即可.在这种情况下,您可以选择 〜 并获得以下信息:
The fix is easy. Just wrap your regex pattern with any valid delimiters. In this case, you could chose ~ and get the following:
~<div[^>]*><ul[^>]*>~
│ │
│ └─ ending delimiter
└───────────────────── starting delimiter
如果尽管使用了定界符但仍收到此错误,则可能是因为模式本身包含了所述定界符的未转义出现.
If you're receiving this error despite having used a delimiter, it might be because the pattern itself contains unescaped occurrences of the said delimiter.
/foo[^/]+bar/i
当然会引发错误.因此,如果它出现在正则表达式中的任何位置,则可以使用 \ 反斜杠对其进行转义:
/foo[^/]+bar/i
would certainly throw an error. So you can escape it using a \ backslash if it appears anywhere within the regex:
/foo[^\/]+bar/i
│ │ │
└──────┼─────┴─ actual delimiters
└─────── escaped slash(/) character
如果您的正则表达式模式包含很多次的定界符,这将是一项繁琐的工作.
This is a tedious job if your regex pattern contains so many occurrences of the delimiter character.
当然,更简洁的方法是完全使用其他定界符.理想情况下,一个字符不会出现在正则表达式模式中的任何位置,例如#
-#foo[^/]+bar#i
.
The cleaner way, of course, would be to use a different delimiter altogether. Ideally a character that does not appear anywhere inside the regex pattern, say #
- #foo[^/]+bar#i
.
- PHP regex分隔符
- http://www.regular-expressions.info/php.html
- 如何在PHP中将ereg表达式转换为preg?(缺少分隔符)
- …中的未知修饰符"/"?是什么?(使用
preg_quote()
)
- PHP regex delimiters
- http://www.regular-expressions.info/php.html
- How can I convert ereg expressions to preg in PHP? (missing delimiters)
- Unknown modifier '/' in …? what is it? (on using
preg_quote()
)
这篇关于警告:preg_replace():未知修饰符']'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!