如何只在字符串中查找完整单词

时间:2012-01-17 17:14:26

标签: php

如何仅在字符串

中查找完整单词
<?php
$str ="By ILI LIKUALAYANA MOKHTAKUALAR AND G. SURACH Datuk Dr Hasan Ali says he has no intention of joining Umno. Pic by Afendi Mohamed KUALA LUMPUR: FORMER Selangor Pas commissioner Datuk Dr Hasan Ali has not ruled out the possibility of returning to Pas' fold";
$search ="KUALA";
?>

5 个答案:

答案 0 :(得分:14)

要查看特定单词是否在字符串中,您可以使用带有单词边界的正则表达式,如下所示:

$str = "By ILI LIKUALAYANA MOKHTAKUALAR AND G. SURACH Datuk Dr Hasan Ali says he has no intention of joining Umno. Pic by Afendi Mohamed KUALA LUMPUR: FORMER Selangor Pas commissioner Datuk Dr Hasan Ali has not ruled out the possibility of returning to Pas' fold";
$search = "KUALA";

if (preg_match("/\b$search\b/", $str)) {
    // word found
}

这里\b表示“单词的边界”。它实际上并不匹配任何字符,只是边界,因此它匹配单词和空格之间的边缘,并且还匹配字符串末尾的边缘。

如果您需要使其不区分大小写,可以将i添加到匹配字符串的末尾,如下所示:"/\b$search\b/i"

如果您需要知道结果在字符串中的哪个位置,您可以添加第三个$matches参数,该参数提供有关匹配的详细信息,如下所示:

if (preg_match("/\b$search\b/", $str, $matches)) {
    // word found
    $position = $matches[1];
}

答案 1 :(得分:5)

只需拆分字符串:

$words = explode(' ',$str);
if (in_array($search,$words)){
    echo "FOUND!";
}

或者,如果您需要该位置:

$words = explode(' ',$str);
$exists_at = array_search($seach,$words);
if ($exists_at){
   echo "Found at ".$exists_at." key in the \$word array";
}

编辑:

鉴于正在进行的正则表达式反正则表达式斗争正在进行中,我必须撤回这个答案,并推迟到正则表达式人群,但我将把它留下历史记录。我一直认为从处理的角度来看,使用数组效率更高,但我决定进行一些测试,结果证明我的假设是错误的。单词测试的结果:

Time to search for word 10000 times using in_array(): 0.011814

Time to search for word 10000 times using preg_match(): 0.001697

我用来测试的代码(假设每次都会使用爆炸):

$str ="By ILI LIKUALAYANA MOKHTAKUALAR AND G. SURACH Datuk Dr Hasan Ali says he has no intention of joining Umno. Pic by Afendi Mohamed KUALA LUMPUR: FORMER Selangor Pas commissioner Datuk Dr Hasan Ali has not ruled out the possibility of returning to Pas' fold";
$search ="KUALA";

$start = microtime(true);
for($i=0;$i<1000;$i++){
    $words = explode(' ',$str);
    if (in_array($search,$words)){
        //
    }

}

$end = microtime();

$total = $end-$start;
echo "Time to search for word 10000 times using in_array(): ";
echo $total;

echo "<br />";

$start = microtime(true);
for($i=0;$i<1000;$i++){
    if (preg_match("/\b$search\b/", $str)) {
        // word found
    }
}

$end = microtime();

$total = $end-$start;
echo "Time to search for word 10000 times using preg_match(): ";
echo $total;

结论:使用preg_match(“/ \ b $ search \ b /”,$ str)

答案 2 :(得分:2)

$str ="By ILI LIKUALAYANA MOKHTAKUALAR AND G. SURACH Datuk Dr Hasan Ali says he has no intention of joining Umno. Pic by Afendi Mohamed KUALA LUMPUR: FORMER Selangor Pas commissioner Datuk Dr Hasan Ali has not ruled out the possibility of returning to Pas' fold";
$search ="KUALA";
$pattern='/\b$search\b/';
if (preg_match($pattern, $str, $matches)) echo "FOUND AT POSITION ".$matches[1];
else echo "NOT FOUND"

答案 3 :(得分:1)

或使用RegExpr:

$str = "The text in the first post";
$search = "first";
if( preg_match('/\b'.$search.'\b/', $str) ) {
  echo 'FOUND!';
}

答案 4 :(得分:1)

使用PHP函数stripos()。

$str = "These aren't the droids you are looking for.";
$search = "droids";
$pos = stripos($str, $search);

$ pos现在将等于17,即字符串开始的位置。如果您正在寻找完全相同的单词,那么请使用strpos()函数; stripos()不区分大小写。如果函数找不到该单词,则返回FALSE。

您可以使用它来确定字符串是否包含单词。

if(stripos($str, $search)){
    echo("It contains the word!");
}else{
    echo("Word not found.");
}

如果你想检查它本身是否存在于字符串中(不是另一个单词的一部分),那么你应该看一下正则表达式。类似的东西:

$str = "These aren't the droids you are looking for. This droid is.";
$search = "droid";
if (preg_match("/\b$search\b/", $str, $match)) {
    $result = $match[0];
}

这将匹配此单词的索引,但不会在其他单词中使用时。所以,在这个例子中:

$result == 51

即使搜索词出现在字符串的前面。

http://php.net/manual/en/function.strpos.php