PHP code example of siugnur / whatlang-php

1. Go to this page and download the library: Download siugnur/whatlang-php 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/ */

    

siugnur / whatlang-php example snippets


use Whatlang\Detector;
use Whatlang\Lang;
use Whatlang\Script;

$detector = Detector::new();

$info = $detector->detect("Hello World");

echo $info->lang();       // Lang::Eng
echo $info->script();     // Script::Latin
echo $info->confidence(); // 1.0
echo $info->isReliable(); // true

$detector = Detector::new();

$lang = $detector->detectLang("Bonjour le monde");
echo $lang; // Lang::Fra

$detector = Detector::new();

$script = $detector->detectScript("Привет мир");
echo $script; // Script::Cyrillic

use function Whatlang\{detect, detectLang, detectScript};

$info = detect("こんにちは世界");
echo $info->lang(); // Lang::Jpn

$lang = detectLang("你好世界");
echo $lang; // Lang::Cmn

$detector = Detector::withAllowlist([Lang::Eng, Lang::Spa, Lang::Fra]);

$lang = $detector->detectLang("Hello World");
// 如果白名单包含 Eng,返回 Eng,否则返回 null

$detector = Detector::withDenylist([Lang::Jpn, Lang::Kor]);

$lang = $detector->detectLang("こんにちは");
echo $lang; // null (日文被排除)

// 获取语言属性
Lang::Eng->code();    // "eng"
Lang::Eng->name();     // "English"
Lang::Eng->engName();  // "English"

// 通过代码获取语言
Lang::fromCode("eng");  // Lang::Eng
Lang::fromCode("zho");  // Lang::Cmn
bash
composer