KwangKung
KwangKung

Reputation: 169

How can search same string with PHP

search text in string but I try not complete.

I want search "2"($word) in string $a

$a = "1,22,3,4,5,6,7,8,9";
$word = "2";

I try code below (not complete)

if(similar_text($a,$word))
{ echo "Found.<br>"; }
else { echo "Not Found.<br>"; }


if(strchr($a,$word))
{ echo "Found.<br>"; }
else { echo "Not Found.<br>"; }

if(strpos($a,$word))
{ echo "Found.<br>"; }
else { echo "Not Found.<br>"; }

Upvotes: 1

Views: 92

Answers (1)

Kevin
Kevin

Reputation: 41885

If you're trying to search the exact 2, then suppose you could explode them first, then in_array() just like @barmar has said.

$a = "1,22,3,4,5,6,7,8,9";
$word = "2";
$a = explode(',', $a);
if(in_array($word, $a)) {
    // Found!
    echo "$word is found in \$a";
} else {
    // sorry not found
    echo "$word is NOT found in \$a";
}

Upvotes: 1

Related Questions