Check if a string contains a certain number

前端 未结 5 1931
死守一世寂寞
死守一世寂寞 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:33

    Another way, that might be more efficient for laaaaaaaarge strings, is using a regexp:

    $numberA = "13";
    $string = "8,7,13,14,16";
    
    if(preg_match('/(^|,)'.$numberA.'($|,)/', $string)){
        $result = "Yeah, that number is in there";
    } else {
        $result = "Sorry.";
    }
    

提交回复
热议问题