警告:preg_replace():未知修饰符 [英] Warning: preg_replace(): Unknown modifier
问题描述
我有以下错误:
<块引用>警告:preg_replace():xxx.php 第 38 行中的未知修饰符]"
这是第 38 行的代码:
<?php echo str_replace("</ul></div>", "", preg_replace("<div[^>]*><ul[^>]*>", "", wp_nav_menu(array('theme_location' => 'nav', 'echo' => false))));?>
我该如何解决这个问题?
为什么会出现错误
在 PHP 中,正则表达式需要包含在一对分隔符.分隔符可以是任何非字母数字、非反斜杠、非空白字符;/
、#
、~
是最常用的.请注意,也可以使用括号样式分隔符,其中开始和结束括号是开始和结束分隔符,即
、[pattern_goes_here]
等.全部有效.
Unknown modifier X"错误通常发生在以下两种情况:
当您的正则表达式缺少分隔符时.
当您在模式中使用分隔符时不转义它.
在这种情况下,正则表达式为
- ]*>.正则表达式引擎将
<
到 >
的所有内容视为正则表达式模式,然后将所有内容视为修饰符.正则表达式:<div[^>]*>]*>│ │ │ │└──┬──┘ └────┬──────┘模式修饰符
]
这里是一个未知的修饰符,因为它出现在结束的 >
分隔符之后.这就是 PHP 抛出该错误的原因.
根据模式,未知修饰符投诉也可能是关于*
、+
、p
、/
或 )
或几乎任何其他字母/符号.只有 imsxeADSUXJu
是有效的 PCRE 修饰符.
如何解决
修复很容易.只需用任何有效的分隔符包装您的正则表达式模式.在这种情况下,您可以选择 ~ 并获得以下内容:
~]*>]*>~│ ││ └─ 结束分隔符└─────────────────────── 起始分隔符如果您在使用分隔符后仍收到此错误,可能是因为模式本身包含未转义的分隔符.
或转义分隔符
/foo[^/]+bar/i
肯定会抛出错误.因此,如果它出现在正则表达式中的任何位置,您可以使用 \ 反斜杠对其进行转义:
/foo[^\/]+bar/i│ │ │└──────┼─────┴─ 实际分隔符└─────── 转义斜线(/)字符
如果您的正则表达式模式包含如此多的分隔符,这将是一项乏味的工作.
当然,更简洁的方法是完全使用不同的分隔符.理想情况下,该字符不会出现在正则表达式模式中的任何位置,例如 #
- #foo[^/]+bar#i
.
更多阅读:
- PHP 正则表达式分隔符
- http://www.regular-expressions.info/php.html
- 如何在 PHP 中将 ereg 表达式转换为 preg?(缺少分隔符)
- ......中的未知修饰符/"?它是什么?(关于使用
preg_quote()代码>
)
I have the following error:
Warning: preg_replace(): Unknown modifier ']' in xxx.php on line 38
This is the code on line 38:
<?php echo str_replace("</ul></div>", "", preg_replace("<div[^>]*><ul[^>]*>", "", wp_nav_menu(array('theme_location' => 'nav', 'echo' => false)) )); ?>
How can I fix this problem?
解决方案 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.
The "Unknown modifier X" error usually occurs in the following two cases:
When your regular expression is missing delimiters.
When you use the delimiter inside the pattern without escaping it.
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
]
here is an unknown modifier, because it appears after the closing >
delimiter. Which is why PHP throws that error.
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.
How to fix it
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.
Or escape delimiters
/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.
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
.
More reading:
- 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屋!
查看全文