How do I detect non-ASCII characters in a string?

前端 未结 9 2291
面向向阳花
面向向阳花 2020-12-01 07:56

If I have a PHP string, how can I determine if it contains at least one non-ASCII character or not, in an efficient way? And by non-ASCII character, I mean any character tha

9条回答
  •  感情败类
    2020-12-01 08:03

    You can use mb_detect_encoding and check for ASCII:

    mb_detect_encoding($str, 'ASCII', true)
    

    This will return false if $str contains at least one non-ASCI character (byte value > 0x7F).

提交回复
热议问题