承接 onnov/detect-encoding 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version License

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 onnov/detect-encoding 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.15M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 23
  • 点击次数: 37
  • 依赖项目数: 9
  • 推荐数: 0

GitHub 信息

  • Stars: 22
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-09-03