Detect language from string in PHP

Beier picture Beier · Sep 18, 2009 · Viewed 62.5k times · Source

In PHP, is there a way to detect the language of a string? Suppose the string is in UTF-8 format.

Answer

scott picture scott · Apr 5, 2010

I've used the Text_LanguageDetect pear package with some reasonable results. It's dead simple to use, and it has a modest 52 language database. The downside is no detection of Eastern Asian languages.

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);
}

results in:

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