Home » Php » php – How do I detect non-ASCII characters in a string?

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

Posted by: admin April 23, 2020 Leave a comment


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.

How to&Answers:

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)


You could use:


but it will be maybe not as precise as you want it to be.


Try: (Source)

function is_ascii( $string = '' ) {
    return ( bool ) ! preg_match( '/[\x80-\xff]+/' , $string );

Although, all of the above answers are correct, but depending upon the input, these solutions may give wrong answers. See the last section in this ASCII validation post.


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


I suggest you look into utf8_encode or utf8_decode under PHP’s manual:


Look into the examples down below as it may have something there that leads you to the right direction if not finding what you are looking for.


If you do not want to deal with Regex in javascript you can do

detectUf8 : function(s) {
  var utf8=s.split('').filter(function(C) {
    return C.charCodeAt(0)>127;
  return (utf8.join('').length>0);