Check if a string contains a certain number

前端 未结 5 1974
死守一世寂寞
死守一世寂寞 2021-01-14 18:14

I have a string

8,7,13,14,16

Whats the easiest way to determine if a given number is present in that string?

$numberA = \"1         


        
5条回答
  •  梦谈多话
    2021-01-14 18:35

    Make sure you match the full number in the string, not just part of it.

    function numberInList($num, $list) {
        return preg_match("/\b$num\b/", $list);
    }
    $string = "8,7,13,14,16";    
    numberInList(13, $string); # returns 1
    numberInList(8, $string); # returns 1
    numberInList(1, $string); # returns 0
    numberInList(3, $string); # returns 0
    

提交回复
热议问题