Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect language from string in PHP

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

like image 562
Beier Avatar asked Sep 17 '09 22:09

Beier


People also ask

How to detect text language in PHP?

You can see how to detect language for a string in php using the Text_LanguageDetect Pear Package or downloading to use it separately like a regular php library.


1 Answers

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 ) 
like image 90
scott Avatar answered Oct 03 '22 00:10

scott