简体   繁体   English

PHP preg_match_all带有“ /”字符的奇怪行为

[英]PHP preg_match_all strange behaviour with “/” character

Using : 使用:

preg_match_all(
    "/\b".$KeyWord."\b/u", 
    $SearchStr,
    $Array1,
    PREG_OFFSET_CAPTURE);

This code works fine for all cases except when there is a / in the $KeyWord var. 此代码适用于所有情况,除非$KeyWord变量中包含/ Then I get a warning and unsuccessful match of course. 然后,我当然会收到警告和不成功的比赛。

Any idea how to work around this? 任何想法如何解决这个问题?

Thanks 谢谢

use preg_quote() around the keyword. 在关键字周围使用preg_quote()

http://us2.php.net/preg_quote http://us2.php.net/preg_quote

but also provide your delimiter, so it gets escaped: preg_quote($KeyWord, "/") 而且还提供了定界符,因此可以将其转义: preg_quote($KeyWord, "/")

您必须解析$ KeyWord并在所有规范符号前添加“ \\”,然后才能使用preg_quote()

Dynamic Values In Patterns 模式中的动态值

You are using a dynamic value inside the pattern. 您正在模式内部使用动态值。 Like escaping for SQL or HTML, a specific escaping for the value is needed. 就像对SQL或HTML进行转义一样,需要对该值进行特定的转义。 If you do not escape meta characters inside the value are interpreted by the regex engine. 如果不转义,则值内的元字符将由正则表达式引擎解释。 The escaping function for PCRE patterns is preg_quote() . PCRE模式的转义函数是preg_quote()

preg_match_all(
    "(\b".preg_quote($KeyWord)."\b)u", 
    $SearchStr,
    $Array1,
    PREG_OFFSET_CAPTURE
);

Delimiters 定界符

The syntax of a pattern in PHPs preg_* function is: PHP的preg_ *函数中模式的语法为:

DELIMITER PATTERN DELIMITER OPTIONS

The / is the delimiter in your pattern. /是模式中的定界符。 So the / inside the $keyWord was recognized as the closing delimiter. 因此,$ keyWord中的/被识别为结束定界符。

But all non alphanumeric characters can be used. 但是可以使用所有非字母数字字符。 In Perl and JS you can define a regular expression directly (not as string) using / so it is often the default in tutorials. 在Perl和JS中,您可以使用/直接定义正则表达式(而不是字符串),因此通常是教程中的默认值。

Most delimiters have to be escaped inside the pattern. 大多数定界符必须在模式内部转义。

  • Match a \\: '/\\//' 匹配一个\\: '/\\//'

The exception to this rule are brackets. 此规则是括号中的例外。 You use any of the bracket pairs as delimiter. 您可以使用任何一个括号对作为分隔符。 And because it is a pair, they can still be used inside the pattern. 而且由于是一对,因此它们仍可以在模式内部使用。

  • Match a \\: '(/)' 匹配一个\\: '(/)'

The () brackets are a good decision, you can count them as "subpattern 0". 方括号()是个不错的选择,您可以将其视为“子模式0”。

You can use preg_quote to handle the backslash character. 您可以使用preg_quote处理反斜杠字符。

From the manual: 从手册中:

puts a backslash in front of every character that is part of the regular expression syntax 在正则表达式语法的每个字符前加一个反斜杠

You can also pass the delimiter as the second parameter and it will also be escaped. 您还可以将定界符作为第二个参数传递,并且也将对其进行转义。 However, if you're using # as your delimiter, then there's no need to escape / 但是,如果您使用#作为分隔符,则无需转义/

So, you can either use: 因此,您可以使用:

preg_match_all("/\b".preg_quote($KeyWord, "/")."\b/u", $SearchStr,$Array1,PREG_OFFSET_CAPTURE))

or, if you are sure that your keyword does not contain any other regex-special characters, you can simply change the delimiter, and use to escape the backslash: 或者,如果您确定关键字中不包含任何其他正则表达式特殊字符,则只需更改分隔符并使用转义符即可:

preg_match_all("#\b".$KeyWord."\b#u", $SearchStr,$Array1,PREG_OFFSET_CAPTURE))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM