PHP 在文本中查找字符串

PHP Find String inside Text

在具有以下内容的文本中查找字符串(PHP)的 best/easiest 方法是什么:

一些例子:

$text = "This is a test èsàdò string123?"; 
$find = "èsàDò"; 

会returntrue

$text = "This is a test èsàdò string123?"; 
$find = "a TEST èsàdò";

会returntrue

$text = "This is a test èsàdò string123?";
$find = "string123";

会returntrue

$text = "This is a test èsàdò string123?";
$find = "string12";

会returnfalse

$text = "This is a test èsàdò string123?";
$find = "This is a test èsàdò String123?";

会returntrue

在您的 'boundary'/定界符处展开,然后遍历数组并在第一个匹配项上 return。

$array = explode(" ", $string);
foreach($array as $array_item){
    if (stripos($array_item, 'èsàdò') !== false) {
        return true;
     }
}

您可以使用 preg_match 或 preg_match_all

等正则表达式函数

示例:

样本: 编辑:(感谢 Thomas Ghesquière 在下方找到了工作模式)

function test($string,$pattern){
   return (preg_match("/\b({$pattern})\b/ui","".trim($string)."",$res)) ? "Found" : "Not found";

}



$text = "This is a test èsàdò string123?"; 
$find = "èsàDò"; 
echo test($text,$find) . "\n";


$text = "This is a test èsàdò string123?"; 
$find = "a TEST èsàdò";
echo test($text,$find) . "\n";


$text = "This is a test èsàdò string123?";
$find = "string123";
echo test($text,$find) . "\n";


$text = "This is a test èsàdò string123?";
$find = "string12";
echo test($text,$find) . "\n";


$text = "This is a test èsàdò string123?";
$find = "This is a test èsàdò String123?";
echo test($text,$find) . "\n";  

将 return 真假,取决于 $text 和 $find。这里它将 return 为真。 http://php.net/manual/fr/function.preg-match.php

sizrar@ovador:~$ php test.php

找到

找到

找到

未找到

找到

你可以看到结果几乎没问题(最后一个例子还有一些调整,但主要原则是存在的) 在这种特殊情况下,您可以轻松排除测试函数中的相等情况。但是要确定要使用的正则表达式,您可以使用像这样的在线测试器:https://regex101.com/ 并相应地完成 preg_match 的第一个参数

匹配以下任何一项:

  • 任何一种特殊字符
  • 字符串边界(space)
  • 不区分大小写

我会调查 preg_match preg_match

然后您可以使用正则表达式搜索与特殊字符或字符串边界匹配的任何字符串,如果它设置了 i 标志,它将不区分大小写。

例如: preg_match("/([èsàDò\b]+)/i"'This is a test èsàdò string123?',$matches) 这会将所有匹配项检索到 $matches 数组中,或者只是一个布尔值 true/false:

if(preg_match("/[èsàDò\b]+/i"'This is a test èsàdò string123?')){
[CODE HERE]
}

对正则表达式使用 \b 单词分隔符,对匹配重音字符使用 u

$text = "This is a test èsàdò string123?";
$find = "èsàDò";
// remove trailing punctuations from $find
// punctuations inside the string are not affected
$find = preg_replace('/([[:punct:]]+)$/iu', '', $find);
// escape any regex specific character
$find = preg_quote($find);

if (preg_match("/\b({$find})\b/ui", $text)) {
    echo "match";
} else {
    echo "no match";
}

由于您没有定义所谓的 "boundary",我假设(根据您的测试)这意味着搜索到的子字符串可以用 space、标点符号和限制字符串。为了表达我使用了负面环视:

  • (?<![^\s\pP]) (前面没有非 space 或标点符号的字符)
  • (?![^\s\pP]) (后跟不是 space 或标点符号的字符)

请注意,我使用了负环视(而不是 (?<=[\s\pP])(?=[\s\pP]))来隐含地包括搜索字符串在字符串的一个限制处开始或结束的情况。换句话说:

  • (?<![^\s\pP]) <=> (?<=[\s\pP]|^)
  • (?![^\s\pP]) <=> (?=[\s\pP]|$)

代码:

$text = 'This is a test èsàdò string123?'; 
$needles = ['èsàDò', 'a TEST èsàdò', 'string123', 'string12', 'This is a test èsàdò String123?'];

$format = " %-35s\t%s%s";
echo 'TEST STRING: ', $text, PHP_EOL, PHP_EOL;
printf($format, 'needle', 'result', PHP_EOL);
echo str_repeat('-', 50), PHP_EOL;

foreach ($needles as $needle) {
    $pattern = '~(?<![^\s\pP])' . preg_quote($needle, '~') . '(?![^\s\pP])~iu';
    printf($format, $needle, (preg_match($pattern, $text) ? 'true' : 'false'), PHP_EOL);
}

结果:

TEST STRING: This is a test èsàdò string123?

 needle                                 result
--------------------------------------------------
 èsàDò                                  true
 a TEST èsàdò                           true
 string123                              true
 string12                               false
 This is a test èsàdò String123?        true