1

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>"; }
3
  • 1
    Split the string into an array, then use in_array(). Commented Oct 3, 2014 at 2:15
  • 1
    so what do you expect then? whats the question/problem? Commented Oct 3, 2014 at 2:15
  • var_dump(preg_match('/(^|,)' . preg_quote($word) . '(,|$)/', $a, $matches)); Commented Oct 3, 2014 at 2:30

1 Answer 1

1

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";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.