PHP code example of pear / text_languagedetect

1. Go to this page and download the library: Download pear/text_languagedetect library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pear / text_languagedetect example snippets


      $text = 'Was wäre, wenn ich Ihnen das jetzt sagen würde?';

    $ld = new Text_LanguageDetect();
    $language = $ld->detectSimple($text);

    echo $language;
    //output: german

Show the three most probable languages with their confidence score::

    
      $text = 'Was wäre, wenn ich Ihnen das jetzt sagen würde?';

    $ld = new Text_LanguageDetect();
    //3 most probable languages
    $results = $ld->detect($text, 3);

    foreach ($results as $language => $confidence) {
        echo $language . ': ' . number_format($confidence, 2) . "\n";
    }

    //output:
    //german: 0.35
    //dutch: 0.25
    //swedish: 0.20
    

     $ld = new Text_LanguageDetect();

    //will output the ISO 639-1 two-letter language code
    // "de"
    $ld->setNameMode(2);
    echo $ld->detectSimple('Das ist ein kleiner Text') . "\n";

    //will output the ISO 639-2 three-letter language code
    // "deu"
    $ld->setNameMode(3);
    echo $ld->detectSimple('Das ist ein kleiner Text') . "\n";