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

I found it more useful to detect if any character falls out of the list

if(preg_match('/[^\x20-\x7e]/', $string))

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).


Try (mb_detect_encoding)


The function ctype_print returns true iff all characters fall into the ASCII range 32-126 (PHP unit test).