Search code examples
phpstrpos

Can we use Bitwise operator "|" with strpos in php?


Can we use Bitwise operator "|" with strpos in php? I need to check if a0,a1,a2,a5 strings are exists in the given $status variable. My code is given bellow.My code will return values(position) only when the status variable have value=a0 or a1 or a2 or a5.It will return false when $status='a1 test string.

 $status='a1 test string';
 echo strpos("|a0|a1|a2|a5|", $status);

Solution

  • No, you cannot. Documentation does not mention anything remotely similar:

    strpos — Find the position of the first occurrence of a substring in a string

    Find the numeric position of the first occurrence of needle in the haystack string.

    Parameters

    haystack The string to search in.

    needle If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.

    offset If specified, search will start this number of characters counted from the beginning of the string. If the offset is negative, the search will start this number of characters counted from the end of the string.

    In fact, it wouldn't make much sense to implement such feature since you already have a full-fledged regular expression engine:

    $has_substrings = (bool)preg_match('/a0|a1|a2|a5/u', $status);