We have a string consisting of several numbers separated by commas:

$allNam = '12,2,41,51,8,53'; 

It is necessary to find the number 51 in the $ allNam variable and if it is there to output True.

    2 answers 2

    1 option

     function search($str, $search) { return false !== stripos($str, $search); } $allNam = '12,2,41,51,8,53'; $result = search($allNam, 51); 

    Option 2

     $allNam = '12,2,41,51,8,53'; $result = in_array(51, explode(',', $allNam)); 

      You could add a рСгулярныС выраТСния label to this question рСгулярныС выраТСния

       (^|[^\d]+)51([^\d]+|$) 

      https://regex101.com/r/QvkJhX/1

       (^|[^\d]+) Π² Π½Π°Ρ‡Π°Π»Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΈΠ»ΠΈ Π½Π°Ρ‡Π°Π»ΠΎ строки ΠΈΠ»ΠΈ Π½Π΅ Ρ†ΠΈΡ„Ρ€Ρ‹ 51 искомоС число ([^\d]+|$) Ρ‚Π°ΠΊ ΠΆΠ΅ ΠΊΠ°ΠΊ ΠΈ Π² Π½Π°Ρ‡Π°Π»Π΅ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΊΠΎΠ½Π΅Ρ† строки 

      The function itself http://php.net/manual/ru/function.preg-match.php

       function testNumber( $n, $subject ) { $reg = '/(^|[^\d]+)'. $n .'([^\d]+|$)/'; return preg_match ( $reg , $subject ); } var_dump(testNumber( 53, '12,2,41,51,8,53' )); // int 1 var_dump(testNumber( 10, '12,2,41,51,8,53' )); // int 0 var_dump(testNumber( 12, '12,2,41,51,8,53' )); // int 1