onnov/detect-encoding
Composer 安装命令:
composer require onnov/detect-encoding
包简介
Text encoding definition class instead of mb_detect_encoding. Defines: utf-8, windows-1251, koi8-r, iso-8859-5, ibm866, .....
关键字:
README 文档
README
Detect encoding
Text encoding definition class based on a range of code page character numbers.
So far, in PHP v7.* the mb_detect_encoding function does not work well.
Therefore, you have to somehow solve this problem.
This class is one solution.
Built-in encodings and accuracy:
| letters -> | 5 | 15 | 30 | 60 | 120 | 180 | 270 |
|---|---|---|---|---|---|---|---|
| windows-1251 | 99.13 | 98.83 | 98.54 | 99.04 | 99.73 | 99.93 | 100.0 |
| koi8-r | 99.89 | 99.98 | 100.0 | 100.0 | 100.0 | 100.0 | 100.0 |
| iso-8859-5 | 81.79 | 99.27 | 99.98 | 100.0 | 100.0 | 100.0 | 100.0 |
| ibm866 | 99.81 | 99.99 | 100.0 | 100.0 | 100.0 | 100.0 | 100.0 |
| MacCyrillic | 12.79 | 47.49 | 73.48 | 92.15 | 99.30 | 99.94 | 100.0 |
Worst accuracy with MacCyrillic, you need at least 60 characters to determine this encoding with an accuracy of 92.15%. Windows-1251 encoding also has very poor accuracy. This is because the numbers of their characters in the tables overlap very much.
Fortunately, MacCyrillic and ibm866 encodings are not used to encode web pages. By default, they are disabled in the script, but you can enable them if necessary.
| letters -> | 5 | 10 | 15 | 30 | 60 |
|---|---|---|---|---|---|
| windows-1251 | 99.40 | 99.69 | 99.86 | 99.97 | 100.0 |
| koi8-r | 99.89 | 99.98 | 99.98 | 100.0 | 100.0 |
| iso-8859-5 | 81.79 | 96.41 | 99.27 | 99.98 | 100.0 |
The accuracy of the determination is high even in short sentences from 5 to 10 letters. And for phrases from 60 letters, the accuracy of determination reaches 100%.
Determining the encoding is very fast, for example, text longer than 1,300,000 Cyrillic characters is checked in 0.00096 sec. (on my computer)
Link to the idea: http://patttern.blogspot.com/2012/07/php-python.html
Installation
Composer (recommended) Use Composer to install this library from Packagist: onnov/captcha
Run the following command from your project directory to add the dependency:
composer require onnov/detect-encoding
Alternatively, add the dependency directly to your composer.json file:
{
"require": {
"onnov/detect-encoding": "^1.0"
}
}
The classes in the project are structured according to the PSR-4 standard, so you can also use your own autoloader or require the needed files directly in your code.
Usage
use Onnov\DetectEncoding\EncodingDetector; $detector = new EncodingDetector();
- Definition of text encoding:
use Onnov\DetectEncoding\EncodingDetector; $detector = new EncodingDetector(); $text = 'Проверяемый текст'; $detector->getEncoding($text);
- Method for converting text of an unknown encoding into a given encoding, by default in utf-8 optional parameters:
use Onnov\DetectEncoding\EncodingDetector; $detector = new EncodingDetector(); /** * Method for converting text of an unknown encoding into a given encoding, by default in utf-8 * optional parameters: * $extra = '//TRANSLIT' (default setting) , other options: '' or '//IGNORE' * $encoding = 'utf-8' (default setting) , other options: any encoding that is available iconv * * @param string $text * @param string $extra * @param string $encoding * * @return string * @throws RuntimeException */ $detector->iconvXtoEncoding($text);
- Method to enable encoding definition:
use Onnov\DetectEncoding\EncodingDetector; $detector = new EncodingDetector(); $detector->enableEncoding([ EncodingDetector::IBM866, EncodingDetector::MAC_CYRILLIC, ]);
- Method to disable encoding definition:
use Onnov\DetectEncoding\EncodingDetector; $detector = new EncodingDetector(); $detector->disableEncoding([ EncodingDetector::ISO_8859_5, ]);
- Method for adding custom encoding:
use Onnov\DetectEncoding\EncodingDetector; $detector = new EncodingDetector(); $detector->addEncoding([ 'encodingName' => [ 'upper' => '1-50,200-250,253', // uppercase character number range 'lower' => '55-100,120-180,199', // lowercase character number range ], ]);
- Method to get a custom encoding range:
use Onnov\DetectEncoding\CodePage; // utf-8 encoded alphabet $cyrillicUppercase = 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФЧЦЧШЩЪЫЬЭЮЯ'; $cyrillicLowercase = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя'; $codePage = new CodePage(); $encodingRange = $codePage->getRange($cyrillicUppercase, $cyrillicLowercase, 'koi8-u');
Tests and examples for the project
Symfony use
Add in services.yaml file:
services: Onnov\DetectEncoding\EncodingDetector: autowire: true
onnov/detect-encoding 适用场景与选型建议
onnov/detect-encoding 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.15M 次下载、GitHub Stars 达 22, 最近一次更新时间为 2019 年 09 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「utf-8」 「iconv」 「encoding」 「cyrillic」 「mb_detect_encoding」 「windows-1251」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 onnov/detect-encoding 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 onnov/detect-encoding 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 onnov/detect-encoding 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP port of URLify.js from the Django project. Transliterates non-ascii characters for use in URLs.
Better encoding conversion for PHP
integration of the Panda encoding services into the Symfony2 Framework
A library to validate strings and transliterate to ascii encoding
A PHP class for converting strings between character encodings without relying on iconv
FileBundle manages uploads and manipulations of files for the Kitpages CMS Bundle
统计信息
- 总下载量: 4.15M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 23
- 点击次数: 37
- 依赖项目数: 9
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-09-03