Check if a string contains a certain number

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

    A simple string search should do if you are just checking to find existence of the string. I dont speak php but i think this is how it could be done.

    $mystring = '8,7,13,14,16';
    $findme   = '13';
    
    if (preg_match('/(?>(^|[^0-9])'.$findme.'([^0-9]|$))/', $mystring)) {
        $result = "Yeah, that number is in there";
    } else {
        $result = "Sorry.";
    }
    

提交回复
热议问题