Search code examples
phpregexvalidationpreg-matchdelimited

Match one of two whole integers in a string of pipe-delimited, pipe-wrapped integers


I have stored as |1|7|11|.

I need to use preg_match(( to check |7| is there or |11| is there etc.

How do I do this?


Solution

  • Use the faster strpos if you only need to check for the existence of two numbers.

    if(strpos($mystring, '|7|') !== FALSE AND strpos($mystring, '|11|') !== FALSE)
    {
        // Found them
    }
    

    Or using slower regex to capture the number

    preg_match('/\|(7|11)\|/', $mystring, $match);
    

    Use regexpal to test regexes for free.