Detect language from string in PHP


The language can’t be detected from the character type. There are other ways, but they don’t guarantee complete accuracy. The ‘TextLanguageDetect Pear Package’ can be used with decent accuracy. Below is a sample code for the same −

Example

require_once 'Text/LanguageDetect.php';
$l = new Text_LanguageDetect();
$result = $l->detect($text, 4);
if (PEAR::isError($result)) {
   echo $result->getMessage();
} else {
   print_r($result);
}

Output

This will produce the following output −

Array
(
   [german] => 0.407037037037
   [dutch] => 0.288065843621
   [english] => 0.283333333333
   [danish] => 0.234526748971
)

It is easy to use and has 52 language databases. But the downside is that the Eastern Asian languages can’t be detected using this package.

Updated on: 09-Apr-2020

747 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements