简体   繁体   中英

Find if one string is substr of another using strpos

I have the following statement to check if $word_to_compare is a substring of $word .

if(strpos($word,$word_to_compare)!==false)
    {
            echo"Match found";
            }

The above logic does not return a match for $word_to_compare = achiev and $word = achievement

What am I doing wrong?

I tested this and it worked for me.

<?php
 $word = "achievement";
 $word_to_compare = "achiev";
 if(strpos($word,$word_to_compare)!==false){
   echo"Match found";
 }
?>

What errors are you getting?

Var_dump solved the issue. There was a additional whitespace in one of the variables causing the issue. Using trim helped.

if (strpos($word, $word_to_compare) !== false) 
    echo 'Match Found';
else
    echo 'Match Not Found';

This should work perfectly!!!

In your case:

$word = "achievement";
$word_to_compare = "achiev";

if(strpos($word, $word_to_compare) !== false)
{
    echo "Match found";
}

This returns "Match found" as Result... Please write the code perfectly, it should work...

And, thanks for giving negative point :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM