Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

php

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.

like image 610
rid Avatar asked Jun 27 '11 19:06

rid


People also ask

How do I find a non ASCII character in a string?

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.

How do you find non ASCII characters in python?

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

Which characters are non ASCII?

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.


2 Answers

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

if(preg_match('/[^\x20-\x7e]/', $string))
like image 83
Karolis Avatar answered Oct 09 '22 17:10

Karolis


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

like image 43
Gumbo Avatar answered Oct 09 '22 18:10

Gumbo