How to find full words only in string
Use the PHP function 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.
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
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.
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"
.
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:
if (preg_match("/\b$search\b/", $str, $matches)) {
// word found
$position = $matches[1];
}
$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"
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";
}
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:
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)
Or use RegExpr:
$str = "The text in the first post";
$search = "first";
if( preg_match('/\b'.$search.'\b/', $str) ) {
echo 'FOUND!';
}