Home >Backend Development >PHP Tutorial >How Can PHP Detect the Language of a UTF-8 String?

How Can PHP Detect the Language of a UTF-8 String?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-03 22:32:12997browse

How Can PHP Detect the Language of a UTF-8 String?

PHP Language Detection for UTF-8 Strings

In PHP, detecting the language of a UTF-8 string is a common task. One versatile solution is the Text_LanguageDetect PEAR package.

This package offers simplicity in use, with a database of 52 languages. However, Eastern Asian language detection is not supported.

Usage

To use the Text_LanguageDetect package, follow these steps:

  1. Include the package using require_once 'Text/LanguageDetect.php';
  2. Initialize the language detector object: $l = new Text_LanguageDetect();
  3. Detect the language and specify the number of top results to return: $result = $l->detect($text, 4);

If the detection is successful, you will receive an array with detected languages and their confidence scores. Otherwise, an error message will be displayed.

Example

Consider the following example:

require_once 'Text/LanguageDetect.php';
$l = new Text_LanguageDetect();
$result = $l->detect("Hallo Welt", 4);

if (PEAR::isError($result)) {
    echo $result->getMessage();
} else {
    print_r($result);
}

This code will detect the language of the string "Hallo Welt" and return an array of detected languages and their confidence scores. The array may look like this:

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

The above is the detailed content of How Can PHP Detect the Language of a UTF-8 String?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn