How to find complete words only in a string

How to find complete words only in a 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
source share
5 answers

To find out if a particular word is in a string, you can use a regular expression with layers, for example:

 $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 "word boundary". It does not correspond to any character, only to borders, therefore it corresponds to the edge between words and spaces, and also corresponds to the edges at the ends of lines.

If you need to do this case insensitive, you can add i to the end of the match string as follows: "/\b$search\b/i" .

If you need to know where the result was in the line, you can add the third parameter $matches , which gives details about the match, for example:

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

Just split the line:

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

Or if you need a place:

 $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 regex struggle going on here, I have to cancel this answer and put aside the crowd of regexes, but I'm going to leave it to record the story. I always thought that working with arrays was more efficient in terms of processing, but I decided to conduct some tests, and it turned out that my assumption was wrong. Single word test result:

 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 assumes that explode will be used every 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 to preg_match ("/ \ b $ search \ b /", $ str)

+5
source
 $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" 
+2
source

Or use RegExpr:

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

Use the PHP stripos () function.

 $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 at which the line begins. If you are looking for the word to have exactly the same case, use the strpos () function instead; stripos () is case insensitive. If the function does not find the word, it will return FALSE.

You can use this to determine if 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 a string by itself (is not part of another word), then you should probably look at regular expressions. Sort of:

 $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 the word, but not when it is used inside another word. So in this example:

 $result == 51 

Despite the fact that the search query appeared earlier in the line.

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

+1
source

Source: https://habr.com/ru/post/1391503/


All Articles