There is a string with numbers. Each value is separated by commas.

Finding 21 on line 12,33,32,443,21,335 could be a simple search, but the numbers are not unique and can be repeated, for example:

$int = '26,499,838,221'; if(stristr($int, '21') == TRUE) { echo '21 найдено'; } 

He will find not 21, but 21 in 221. How to solve the problem? Use explode and check every value?

    1 answer 1

    Use regular expressions:

     $need = 21; $str = '26,499,838,221'; if (preg_match("~\b{$need}\b~", $str)) { echo "Найдено $need"; } 

    If you know for sure that numbers are separated only by a comma, then you can also use explode ():

     $need = 21; $str = '26,499,838,221'; if (in_array($need, explode(',', $str))) { echo "Найдено $need"; }