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 that is not part of this table, http://www.asciitable.com/, positions 32 - 126 inclusive.
So not only does it have to be part of the ASCII table, but it also has to be printable. I want to detect a string that contains at least one character that does not meet these specifications (either non-printable ASCII, or a different character altogether, such as a Unicode character that is not part of that table.
The isascii() function returns a boolean value where True indicates that the string contains all ASCII characters and False indicates that the string contains some non-ASCII characters.
Strip Out Non ASCII Characters Python Here we can see how to strip out ASCII characters in Python. In this example, we will use the. sub() method in which we have assigned a standard code '[^\x00-\x7f]' and this code represents the values between 0-127 ASCII code and this method contains the input string 'new_str'.
Non-ASCII characters are those that are not encoded in ASCII, such as Unicode, EBCDIC, etc. ASCII is limited to 128 characters and was initially developed for the English language.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With