如何在字符串中找到完整的单词?

时间:2021-05-10 19:23:13

How to find full words only in string

如何在字符串中找到完整的单词?

<?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 个解决方案

#1


14  

To see if a particular word is in a string, you can use a regular expression with word boundaries, like so:

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

$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
}

Here \b means "boundary of a word". It doesn't actually match any characters, just boundaries, so it matches the edge between words and spaces, and also matches the edges at the ends of the strings.

这里\b的意思是“一个字的边界”。它并不匹配任何字符,只是边界,所以它匹配单词和空格之间的边,也匹配字符串末端的边。

If you need to make it case-insensitive, you can add i to the end of the match string like this: "/\b$search\b/i".

如果需要不区分大小写,可以将i添加到匹配字符串的末尾,如:“/\b$search\b/i”。

If you need to know where in the string the result was, you can add a third $matches parameter which gives details about the match, like this:

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

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

#2


5  

Just split up the string:

把绳子分开:

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

Or, if you need the location:

或者,如果你需要地点:

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

EDITS:

In light of the pro-regex anti regex fight going on here, I must retract this answer and defer to the regex crowd, but I'm going to leave it up for historical record. I had always assumed that working with arrays was more efficient from a processing standpoint, but I decided to run some tests, and it turns out that my assumption was wrong. The result of a single word test:

鉴于这里正在进行的反regex反regex的战斗,我必须收回这个答案,并遵从regex的人群,但我将把它作为历史记录。我一直假设从处理的角度来看,使用数组更有效,但是我决定运行一些测试,结果证明我的假设是错误的。一个单词测试的结果:

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

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

The code I used to test (which presumes that explode will be used each time):

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

$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;

So conclusion: go with preg_match("/\b$search\b/", $str)

所以结论:go with preg_match("/\b$search\b/", $str)

#3


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"

#4


1  

Use the PHP function stripos().

使用PHP函数stripos()。

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

$pos will now be equal to 17, the position that the string starts at. If you are looking for the word to be of exactly the same case, then use the function strpos() instead; stripos() is case insensitive. If the function doesn't find the word, it will return FALSE.

$pos现在将等于17,字符串开始的位置。如果您正在寻找的单词是完全相同的情况,那么使用函数strpos();(大小写不敏感)是不区分大小写。如果函数没有找到单词,它将返回FALSE。

You can use that to determine whether a string contains a word.

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

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

If you want to check that it exists in the string by itself (not part of another word), then you should probably be looking at regular expressions. Something like:

如果您想要检查它本身是否存在于字符串中(不是另一个词的一部分),那么您可能需要查看正则表达式。喜欢的东西:

$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];
}

This will match the index of this word, but not when it is used inside another word. So, in this example:

这将匹配这个单词的索引,但不是当它在另一个单词中使用时。所以,在这个例子:

$result == 51

Even though the search term appeared earlier in the string.

尽管搜索词出现在字符串的前面。

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

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

#5


1  

Or use RegExpr:

或者使用RegExpr:

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

#1


14  

To see if a particular word is in a string, you can use a regular expression with word boundaries, like so:

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

$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
}

Here \b means "boundary of a word". It doesn't actually match any characters, just boundaries, so it matches the edge between words and spaces, and also matches the edges at the ends of the strings.

这里\b的意思是“一个字的边界”。它并不匹配任何字符,只是边界,所以它匹配单词和空格之间的边,也匹配字符串末端的边。

If you need to make it case-insensitive, you can add i to the end of the match string like this: "/\b$search\b/i".

如果需要不区分大小写,可以将i添加到匹配字符串的末尾,如:“/\b$search\b/i”。

If you need to know where in the string the result was, you can add a third $matches parameter which gives details about the match, like this:

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

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

#2


5  

Just split up the string:

把绳子分开:

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

Or, if you need the location:

或者,如果你需要地点:

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

EDITS:

In light of the pro-regex anti regex fight going on here, I must retract this answer and defer to the regex crowd, but I'm going to leave it up for historical record. I had always assumed that working with arrays was more efficient from a processing standpoint, but I decided to run some tests, and it turns out that my assumption was wrong. The result of a single word test:

鉴于这里正在进行的反regex反regex的战斗,我必须收回这个答案,并遵从regex的人群,但我将把它作为历史记录。我一直假设从处理的角度来看,使用数组更有效,但是我决定运行一些测试,结果证明我的假设是错误的。一个单词测试的结果:

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

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

The code I used to test (which presumes that explode will be used each time):

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

$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;

So conclusion: go with preg_match("/\b$search\b/", $str)

所以结论:go with preg_match("/\b$search\b/", $str)

#3


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"

#4


1  

Use the PHP function stripos().

使用PHP函数stripos()。

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

$pos will now be equal to 17, the position that the string starts at. If you are looking for the word to be of exactly the same case, then use the function strpos() instead; stripos() is case insensitive. If the function doesn't find the word, it will return FALSE.

$pos现在将等于17,字符串开始的位置。如果您正在寻找的单词是完全相同的情况,那么使用函数strpos();(大小写不敏感)是不区分大小写。如果函数没有找到单词,它将返回FALSE。

You can use that to determine whether a string contains a word.

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

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

If you want to check that it exists in the string by itself (not part of another word), then you should probably be looking at regular expressions. Something like:

如果您想要检查它本身是否存在于字符串中(不是另一个词的一部分),那么您可能需要查看正则表达式。喜欢的东西:

$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];
}

This will match the index of this word, but not when it is used inside another word. So, in this example:

这将匹配这个单词的索引,但不是当它在另一个单词中使用时。所以,在这个例子:

$result == 51

Even though the search term appeared earlier in the string.

尽管搜索词出现在字符串的前面。

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

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

#5


1  

Or use RegExpr:

或者使用RegExpr:

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