Yabes Nadar
Yabes Nadar

Reputation: 184

To find complete String is present or not...not sub string

If my str2 (variable) is java. still i get output :-value present. i want to search only complete string not sub string. Is their any function which cheks for only complete string??

     $str2 ="javascript";

     $str1 ="abc def xyz in javascript ";

     if(stripos($str1,$str2) !== false) { 

        echo "value present";   

     }else{

     echo "noooo";

    }

Upvotes: 1

Views: 46

Answers (2)

lalithkumar
lalithkumar

Reputation: 3540

Here if you pass the full name in $str only it will return value present.

    $str1 ="abc def xyz in javascripts ";
    $array=explode(" ",$str1);
    $str='javascripts';
    if (in_array($str,$array)){
        echo "value present"; 
    }else{
        echo "noooo";
    }

Upvotes: 0

Bibhudatta Sahoo
Bibhudatta Sahoo

Reputation: 4894

If you want to search a string like this $str2 ="javascript is awesome ";

Then try below code

$str2 ="javascript is awesome";
$str1 ="abc def xyz in javascript ";
$filterKeys = explode(' ', $str2);
$regexp='';
foreach ($filterKeys as $word) {
    $word = trim($word);
    if ($word) {
        $regexp = $regexp . "$word|";
    }
}
$regexp = chop($regexp, '|');
preg_match("/\b($regexp)\b/i", $str1, $res);
if(!empty($res)) {
    echo "value present";
}else{
    echo "noooo";

}

I think it will help you.

Upvotes: 1

Related Questions