PHP Detect if non-english

Written by Tony Lea on Jun 10th, 2011 Views Report Post

If there ever comes a time when you wish to detect whether a string has english characters or not... There is a very simple way of representing this in PHP. Here is a PHP function which will return true if the string contains english characters or it will return false if there are foreign characters.

function is_english($str)
{
	if (strlen($str) != strlen(utf8_decode($str))) {
		return false;
	} else {
		return true;
	}
}

So, if you were to pass the function:

is_english('hello');

You would receive back a true value. Alternatively if you were to call this function as the following:

is_english('NON-ENGLISH-CHAR');

You will most likely receive a false value.

Comments (0)