whatlang-php:轻量高效的 PHP 语言检测库

发布时间:2026/7/18 16:27:36
whatlang-php:轻量高效的 PHP 语言检测库 whatlang-php 使用指南whatlang-php轻量高效的 PHP 语言检测库项目简介快速开始基本使用只检测语言只检测书写系统使用便捷函数检测结果对象高级用法1. 语言过滤只检测指定语言白名单排除指定语言黑名单2. 语言信息3. 支持的语言实际应用示例示例 1多语言网站内容检测示例 2用户输入语言验证总结whatlang-php轻量高效的 PHP 语言检测库whatlang-php是一个基于 whatlang-rs 库开发的 PHP 扩展/包用于快速、准确地检测文本的语言。它支持检测 70 种语言和 26 种书写系统具有高准确率和极快的检测速度特别适合需要实时语言检测的 PHP 应用场景。项目简介GitHub 仓库SIugnur/whatlang-php主要功能检测给定文本的语言、脚本书写系统和置信度核心优势高准确率即使在短文本上也能保持较高准确率轻量级依赖少易于集成多语言支持支持 70 种语言和 26 种书写系统检测快速开始composerrequire siugnur/whatlang-php基本使用?phprequire_oncevendor/autoload.php;useWhatlang\Detector;useWhatlang\Lang;useWhatlang\Script;$detectorDetector::new();$info$detector-detect(Hello World);echo$info-lang();// Lang::Engecho$info-script();// Script::Latinecho$info-confidence();// 1.0echo$info-isReliable();// true只检测语言$detectorDetector::new();$lang$detector-detectLang(Bonjour le monde);echo$lang;// Lang::Fra只检测书写系统$detectorDetector::new();$script$detector-detectScript(Привет мир);echo$script;// Script::Cyrillic使用便捷函数usefunctionWhatlang\{detect,detectLang,detectScript};$infodetect(こんにちは世界);echo$info-lang();// Lang::Jpn$langdetectLang(你好世界);echo$lang;// Lang::Cmn检测结果对象detect()方法返回一个检测结果对象包含以下方法lang()- 返回语言枚举值如Lang::Engscript()- 返回书写系统枚举值如Script::Latinconfidence()- 返回置信度分数0.0 到 1.0isReliable()- 返回布尔值表示检测是否可靠高级用法1. 语言过滤只检测指定语言白名单$detectorDetector::withAllowlist([Lang::Eng,Lang::Spa,Lang::Fra]);$lang$detector-detectLang(Hello World);// 如果白名单包含 Eng返回 Eng否则返回 null排除指定语言黑名单$detectorDetector::withDenylist([Lang::Jpn,Lang::Kor]);$lang$detector-detectLang(こんにちは);echo$lang;// null (日文被排除)2. 语言信息// 获取语言属性Lang::Eng-code();// engLang::Eng-name();// EnglishLang::Eng-engName();// English// 通过代码获取语言Lang::fromCode(eng);// Lang::EngLang::fromCode(zho);// Lang::Cmn3. 支持的语言whatlang-php 支持 70 种语言包括语言代码语言代码中文zho英语eng俄语rus西班牙语spa法语fra德语deu日语jpn韩语kor阿拉伯语ara印地语hin葡萄牙语por意大利语ita越南语vie土耳其语tur荷兰语nld波兰语pol希腊语ell希伯来语heb波斯语fas泰语tha孟加拉语ben乌克兰语ukr查看全部 70 种语言…实际应用示例示例 1多语言网站内容检测classContentProcessor{private$detector;publicfunction__construct(){$this-detectorDetector::new();}publicfunctionprocessContent($content){$result$this-detector-detect($content);if($result-isReliable()){$langCode$result-langCode();switch($langCode){casezh:return$this-processChinese($content);caseen:return$this-processEnglish($content);caseja:return$this-processJapanese($content);default:return$this-processOther($content,$langCode);}}return$this-processUnknown($content);}// ... 各种语言的处理方法}示例 2用户输入语言验证classUserRegistration{private$detector;private$allowedLanguages[en,zh,fr,es,de];publicfunction__construct(){$this-detectorDetector::new();}publicfunctionvalidateBio($bio){if(empty($bio)){returntrue;}$result$this-detector-detectWithOptions($bio,$this-allowedLanguages);if(!$result-isReliable()){thrownewException(无法识别个人简介的语言请使用支持的语言书写);}returntrue;}}总结whatlang-php是一个强大且高效的 PHP 语言检测库特别适合需要快速、准确语言检测的应用场景。无论是构建多语言网站、内容分析系统还是用户输入验证它都能提供可靠的解决方案。通过简单的 Composer 安装和直观的 API您可以轻松地将语言检测功能集成到您的 PHP 项目中。开始使用composerrequire siugnur/whatlang-php欢迎 Star 项目并参与贡献