[英]preg_match: can't find substring which has trailing special characters
I have a function which uses preg_match to check for if a substring is in another string.我有一个函数,它使用 preg_match 来检查子字符串是否在另一个字符串中。 Today I realize that if substring has trailing special characters like special regular expression characters (. \\ + * ? [ ^ ] $ ( ) { } = ! < > | : -) or @, my preg_match can't find the substring even though it is there.
今天我意识到,如果子字符串有尾随的特殊字符,比如特殊的正则表达式字符 (.\\ + * ? [ ^ ] $ ( ) { } = ! < > | :-) 或 @,我的 preg_match 找不到子字符串,即使它在那里。
This works, returns "A match was found."这有效,返回“找到匹配项”。
$find = "website scripting";
$string = "PHP is the website scripting language of choice.";
if (preg_match("/\b" . $find . "\b/i", $string)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
But this doesn't, returns "A match was not found."但这不会,返回“未找到匹配项”。
$find = "website scripting @";
$string = "PHP is the website scripting @ language of choice.";
if (preg_match("/\b" . $find . "\b/i", $string)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
I have tried preg_quote, but it doesn't help.我试过 preg_quote,但没有帮助。
Thank you for any suggestions!感谢您的任何建议!
Edit: Word boundary is required, that's why I use \\b.编辑:需要单词边界,这就是我使用 \\b 的原因。 I don't want to find "phone" in "smartphone".
我不想在“智能手机”中找到“手机”。
You can just check if the characters around the search word are not word characters with look-arounds:您可以通过环视检查搜索词周围的字符是否不是单词字符:
$find = "website scripting @";
$string = "PHP is the website scripting @ language of choice.";
if (preg_match("/(?<!\\w)" . preg_quote($find, '/') . "(?!\\w)/i", $string)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
See IDEONE demo看IDEONE 演示
Result: A match was found.
结果:
A match was found.
Note the double slash used with \\w
in (?<!\\\\w)
and (?!\\\\w)
, as you have to escape regex special characters in interpolated strings.请注意在
(?<!\\\\w)
和(?!\\\\w)
与\\w
一起使用的双斜杠,因为您必须转义内插字符串中的正则表达式特殊字符。
The preg_quote
function is necessary as the search word - from what I see - can have special characters, and some of them must be escaped if intended to be matched as literal characters. preg_quote
函数是必要的,因为搜索词 - 从我看到的 - 可以有特殊字符,如果打算作为文字字符匹配,其中一些必须被转义。
UPDATE更新
There is a way to build a regex with smartly placed word boundaries around the keyword, but the performance will be worse compared with the approach above.有一种方法可以在关键字周围巧妙地放置单词边界来构建正则表达式,但与上述方法相比,性能会更差。 Here is sample code:
这是示例代码:
$string = "PHP is the website scripting @ language of choice.";
$find = "website scripting @";
$find = preg_quote($find);
if (preg_match('/\w$/u', $find)) { // Setting trailing word boundary
$find .= '\\b';
}
if (preg_match('/^\w/u', $find)) { // Setting leading word boundary
$find = '\\b' . $find;
}
if (preg_match("/" . $find . "/ui", $string)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
If you try to find a string from another string, you can strpos()
.如果您尝试从另一个字符串中查找一个字符串,您可以使用
strpos()
。
Ex.前任。
<?php
$find = "website scripting";
$string = "PHP is the website scripting language of choice.";
if (strpos($string,$find) !== false) {
echo 'true';
} else {
echo 'false';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.