PHP Detect if not English

I use this code to check if a string is in English or not.

<?php $string="γ§ζ›Έγγ‚Ώγƒƒγƒγ‚€γƒ™γƒ³γƒˆ (フ"; if(!preg_match('/[^\W_ ] /',$string)) { echo "Please enter English words only:("; } else { echo "OK, English Detected!"; } ?> 

It cannot provide the perfect result, because a line like "some english text γ§ζ›Έγγ‚Ώγƒƒγƒγ‚€γƒ™γƒ³γƒˆ (フ" also defines English as any idea?

+6
source share
3 answers

Try this (note that you need the mbstring php module):

 <?php $string="γ§ζ›Έγγ‚Ώγƒƒγƒγ‚€γƒ™γƒ³γƒˆ (フ"; if(strlen($string) != mb_strlen($string, 'utf-8')) { echo "Please enter English words only:("; } else { echo "OK, English Detected!"; } ?> 
+17
source

You cannot determine the language from a character type. And there are no reliable ways to do this.

Using any method, you simply get a reasonable guess. From: Detecting a language from a string in PHP

Although some of the articles below may be useful in the case.

http://papermashup.com/php-language-detection/

https://github.com/BruceJillis/PHP-Language-Detection

http://phpir.com/language-detection-with-n-grams/

Hope this helps.

0
source
 if(!preg_match('/[^\w ]/u',$string)) 
0
source

All Articles