PHPackages                             onnov/detect-encoding - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. onnov/detect-encoding

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

onnov/detect-encoding
=====================

Text encoding definition class instead of mb\_detect\_encoding. Defines: utf-8, windows-1251, koi8-r, iso-8859-5, ibm866, .....

v2.0.0(5y ago)223.8M—1%5[1 issues](https://github.com/onnov/detect-encoding/issues)7MITPHPPHP &gt;=7.3

Since Sep 3Pushed 5y ago2 watchersCompare

[ Source](https://github.com/onnov/detect-encoding)[ Packagist](https://packagist.org/packages/onnov/detect-encoding)[ Docs](https://github.com/onnov/detect-encoding)[ RSS](/packages/onnov-detect-encoding/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (9)Versions (6)Used By (7)

[![Build Status](https://camo.githubusercontent.com/cad2a6a0dd8c4c82846ec6ad5eae4fb74b36b9f35f133b87909bffa947de4a72/68747470733a2f2f7472617669732d63692e6f72672f6f6e6e6f762f6465746563742d656e636f64696e672e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/onnov/detect-encoding)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/c8e88914144664e19a91f21047a3fb5a19de99d81c219ce5fa3fa8b477978759/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6f6e6e6f762f6465746563742d656e636f64696e672f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/onnov/detect-encoding/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/01f104e26d41041bac6b06fa5bf1e945615894fb86b709db8a4d55acf5cce6f6/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6f6e6e6f762f6465746563742d656e636f64696e672f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/onnov/detect-encoding/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/a12ee97750cb9f88130f5da014a98008bccb3bf7bc09e72c2e208898aeeb6f5b/68747470733a2f2f706f7365722e707567782e6f72672f6f6e6e6f762f6465746563742d656e636f64696e672f762f737461626c65)](https://packagist.org/packages/onnov/detect-encoding)[![License](https://camo.githubusercontent.com/fc4939f6ca99477c52c335c27d558450004ecc7b4f0a56de9798a23275194f3d/68747470733a2f2f706f7365722e707567782e6f72672f6f6e6e6f762f6465746563742d656e636f64696e672f6c6963656e7365)](https://packagist.org/packages/onnov/detect-encoding)

Detect encoding
===============

[](#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 -&gt;5153060120180270windows-125199.1398.8398.5499.0499.7399.93100.0koi8-r99.8999.98100.0100.0100.0100.0100.0iso-8859-581.7999.2799.98100.0100.0100.0100.0ibm86699.8199.99100.0100.0100.0100.0100.0MacCyrillic12.7947.4973.4892.1599.3099.94100.0Worst 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 -&gt;510153060windows-125199.4099.6999.8699.97100.0koi8-r99.8999.9899.98100.0100.0iso-8859-581.7996.4199.2799.98100.0The 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:

---

Installation
------------

[](#installation)

[Composer](https://getcomposer.org) (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
-----

[](#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](https://github.com/onnov/detect-encoding-test)

---

Symfony use
-----------

[](#symfony-use)

Add in services.yaml file:

```
services:
    Onnov\DetectEncoding\EncodingDetector:
        autowire: true
```

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity53

Moderate usage in the ecosystem

Community24

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~121 days

Total

5

Last Release

1960d ago

Major Versions

v1.2.0 → v2.0.02021-01-04

PHP version history (4 changes)v1.0.1PHP &gt;=5.6.0

v1.1.0PHP &gt;=5.6

v1.2.0PHP &gt;=7.2

v2.0.0PHP &gt;=7.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/13568367?v=4)[onnov](/maintainers/onnov)[@onnov](https://github.com/onnov)

---

Top Contributors

[![yaroslavche](https://avatars.githubusercontent.com/u/17838239?v=4)](https://github.com/yaroslavche "yaroslavche (24 commits)")[![oka-volga](https://avatars.githubusercontent.com/u/3319885?v=4)](https://github.com/oka-volga "oka-volga (14 commits)")[![onnov](https://avatars.githubusercontent.com/u/13568367?v=4)](https://github.com/onnov "onnov (14 commits)")[![jhaoda](https://avatars.githubusercontent.com/u/2151259?v=4)](https://github.com/jhaoda "jhaoda (1 commits)")[![karlerss](https://avatars.githubusercontent.com/u/10937535?v=4)](https://github.com/karlerss "karlerss (1 commits)")

---

Tags

utf-8iconvencodingcyrillicmb\_detect\_encodingwindows-1251koi8-riso-8859-5ibm866

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/onnov-detect-encoding/health.svg)

```
[![Health](https://phpackages.com/badges/onnov-detect-encoding/health.svg)](https://phpackages.com/packages/onnov-detect-encoding)
```

###  Alternatives

[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k394.3M1.5k](/packages/nette-utils)[symfony/polyfill-iconv

Symfony polyfill for the Iconv extension

1.8k352.7M84](/packages/symfony-polyfill-iconv)[paragonie/constant_time_encoding

Constant-time Implementations of RFC 4648 Encoding (Base-64, Base-32, Base-16)

903329.7M148](/packages/paragonie-constant-time-encoding)[danielstjules/stringy

A string manipulation library with multibyte support

2.4k26.0M191](/packages/danielstjules-stringy)[voku/portable-utf8

Portable UTF-8 library - performance optimized (unicode) string functions for php.

52322.4M40](/packages/voku-portable-utf8)[jbroadway/urlify

A fast PHP slug generator and transliteration library that converts non-ascii characters for use in URLs.

6737.4M62](/packages/jbroadway-urlify)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
