Hi All

Perhaps someone has a solution:

I have a string like so:
"1#2#3#4#21#25#"

I want to know of this string contains the character "21"

However using strstr does not work as it always returns false:

$string = "21";
$container = "1#2#3#4#21#25#";

if(strstr($container,$string)) {
     echo "found it.";
} else {
     echo "not found.";
}

Does anyone have any ideas?

Regards
Russ

    Hi,

    the code works fine on my server. But I see another problem. Let's say you want to search for 2 then your code would return true if the string is e.g. 1#5#21# because 21 contains a 2.

    Try the following code:

    if (preg_match("§(^|#)$string(#|$)§",$container)) {
      echo "found it.";
    } else {
      echo "not found.";
    }
    

      I feel stupid I had the variable values reversed.

      Thanks for your help!

      Regards
      Russ

        Why not use explode to put the string into an array? That way, you can check for the numbers individually using the [man]in_array[/man] function, instead of relying on string manipulation:

        example:

        $valuecheck = "21";
        $string = "2#21#35#3#78#12#90#";
        $stringArray = explode("#", $string);
        if(in_array($valuecheck, $stringArray)){
        	echo "Yep, $valuecheck is in there!";
        } else {
        	echo "Nope, $valuecheck is not in there!";
        }
        

        I just suggest this, because I personally like to avoid regular expressions as much as I possibly can. 😃

          Write a Reply...