fab2s / utf8
A purely static UTF8 Helper based on mb_string and ext-intl
Installs: 287 160
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 1
pkg:composer/fab2s/utf8
Requires
- php: ^7.2|^8.0
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ~7.0|~8.0
Suggests
- ext-intl: To use Utf8 Normalization
This package is auto-updated.
Last update: 2025-09-28 01:06:48 UTC
README
A purely static UTF-8 Helper based on mb_string and ext-intl
For sure this is not new, and there is better out there, but this is small and does the job without a lot of noise
Installation
Utf8 can be installed using composer:
composer require "fab2s/utf8"
Utf8 is also included in OpinHelper which packages several bellow "Swiss Army Knife" level Helpers covering some of the most annoying aspects of php programing, such as UTF8 string manipulation, high precision Mathematics or properly locking a file
Should you need to work with php bellow 7.1, you can still use OpinHelper 0.x
Prerequisites
Utf8 requires mb_string, ext-intl is auto detected and used when available for UTF-8 Normalization
In practice
Utf8 offers replacement for most native string functions with support for UTF-8:
-
Utf8::strrpos(string string $str, string $needle, int $offset = null):int|falseUTF-8 aware strrpos() replacement
-
Utf8::strpos(string $str, string $needle, int $offset = 0):int|falseUTF-8 aware strpos() replacement
-
Utf8::strtolower(string $str):stringUTF-8 aware strtolower() replacement
-
Utf8::strtoupper(string $str):stringUTF-8 aware strtoupper() replacement
-
Utf8::substr(string $str, int $offset, int $length = null):stringUTF-8 aware substr() replacement
-
Utf8::strlen(string $str):intUTF-8 aware strlen() replacement
-
Utf8::ucfirst(string $str):stringUTF-8 aware ucfirst() replacement
-
Utf8::ucwords(string $str):stringUTF-8 aware ucwords() replacement
-
Utf8::ord(string $chr):int|falseUTF-8 aware ord() replacement
-
Utf8::chr(int $num):string|falseUTF-8 aware chr() replacement
And some simple utility methods:
-
Utf8::normalize(string $string, int $canonicalForm = self::NORMALIZE_NFC):stringUTF-8 ext-intl Normalizer
WARNING: This method will do nothing in case
ext-intlis not installed on the host This means it is up to you to make sure about it usingUtf8::normalizerSupportor by addingext-intlas a requirement to your project'scomposer.jsonfile -
Utf8::hasUtf8(string $string):boolTells if the input string contains some UTF-8
-
Utf8::isUtf8(string $string):boolTells if the input string is valid UTF-8
-
Utf8::replaceMb4(string $string, string $replace = ''):string -
Utf8::normalizerSupport(bool $disable = false):boolTells if Normalizer is available or disable Normalizer support
Requirements
Utf8 is tested against php 7.2, 7.3, 7.4, 8.0 and 8.1
Contributing
Contributions are welcome, do not hesitate to open issues and submit pull requests.
License
Utf8 is open-sourced software licensed under the MIT license