patchwork/utf8
最新稳定版本:v1.3.3
Composer 安装命令:
composer require patchwork/utf8
包简介
Portable and performant UTF-8, Unicode and Grapheme Clusters for PHP
README 文档
README
Patchwork UTF-8 gives PHP developpers extensive, portable and performant handling of UTF-8 and grapheme clusters.
It provides both :
- a portability layer for
mbstring,iconv, and intlNormalizerandgrapheme_*functions, - an UTF-8 grapheme clusters aware replica of native string functions.
It can also serve as a documentation source referencing the practical problems that arise when handling UTF-8 in PHP: Unicode concepts, related algorithms, bugs in PHP core, workarounds, etc.
Version 1.2 adds best-fit mappings for UTF-8 to Code Page approximations. It also adds Unicode filesystem access under Windows, using preferably wfio or a COM based fallback otherwise.
Portability
Unicode handling in PHP is best performed using a combo of mbstring, iconv,
intl and pcre with the u flag enabled. But when an application is expected
to run on many servers, you should be aware that these 4 extensions are not
always enabled.
Patchwork UTF-8 provides pure PHP implementations for 3 of those 4 extensions.
pcre compiled with unicode support is required but is widely available.
The following set of portability-fallbacks allows an application to run on a
server even if one or more of those extensions are not enabled:
- utf8_encode, utf8_decode,
mbstring: mb_check_encoding, mb_convert_case, mb_convert_encoding, mb_decode_mimeheader, mb_detect_encoding, mb_detect_order, mb_encode_mimeheader, mb_encoding_aliases, mb_get_info, mb_http_input, mb_http_output, mb_internal_encoding, mb_language, mb_list_encodings, mb_output_handler, mb_strlen, mb_strpos, mb_strrpos, mb_strtolower, mb_strtoupper, mb_stripos, mb_stristr, mb_strrchr, mb_strrichr, mb_strripos, mb_strstr, mb_strwidth, mb_substitute_character, mb_substr, mb_substr_count,iconv: iconv, iconv_mime_decode, iconv_mime_decode_headers, iconv_get_encoding, iconv_set_encoding, iconv_mime_encode, ob_iconv_handler, iconv_strlen, iconv_strpos, iconv_strrpos, iconv_substr,intl: Normalizer, grapheme_extract, grapheme_stripos, grapheme_stristr, grapheme_strlen, grapheme_strpos, grapheme_strripos, grapheme_strrpos, grapheme_strstr, grapheme_substr, normalizer_is_normalized, normalizer_normalize.
Patchwork\Utf8
Grapheme clusters should always be
considered when working with generic Unicode strings. The Patchwork\Utf8
class implements the quasi-complete set of native string functions that need
UTF-8 grapheme clusters awareness. Function names, arguments and behavior
carefully replicates native PHP string functions.
Some more functions are also provided to help handling UTF-8 strings:
- filter(): normalizes to UTF-8 NFC, converting from CP-1252 when needed,
- isUtf8(): checks if a string contains well formed UTF-8 data,
- toAscii(): generic UTF-8 to ASCII transliteration,
- strtocasefold(): unicode transformation for caseless matching,
- strtonatfold(): generic case sensitive transformation for collation matching,
- strwidth(): computes the width of a string when printed on a terminal,
- wrapPath(): unicode filesystem access under Windows and other OSes.
Mirrored string functions are: strlen, substr, strpos, stripos, strrpos, strripos, strstr, stristr, strrchr, strrichr, strtolower, strtoupper, wordwrap, chr, count_chars, ltrim, ord, rtrim, trim, str_ireplace, str_pad, str_shuffle, str_split, str_word_count, strcmp, strnatcmp, strcasecmp, strnatcasecmp, strncasecmp, strncmp, strcspn, strpbrk, strrev, strspn, strtr, substr_compare, substr_count, substr_replace, ucfirst, lcfirst, ucwords, number_format, utf8_encode, utf8_decode, json_decode, filter_input, filter_input_array.
Notably missing (but hard to replicate) are printf-family functions.
The implementation favors performance over full edge cases handling. It generally works on UTF-8 normalized strings and provides filters to get them.
As the turkish locale requires special cares, a Patchwork\TurkishUtf8 class
is provided for working with this locale. It clones all the features of
Patchwork\Utf8 but knows about the turkish specifics.
Usage
The recommended way to install Patchwork UTF-8 is through
composer. Just create a composer.json file and run
the php composer.phar install command to install it:
{
"require": {
"patchwork/utf8": "~1.2"
}
}
Then, early in your bootstrap sequence, you have to configure your environment:
\Patchwork\Utf8\Bootup::initAll(); // Enables the portablity layer and configures PHP for UTF-8 \Patchwork\Utf8\Bootup::filterRequestUri(); // Redirects to an UTF-8 encoded URL if it's not already the case \Patchwork\Utf8\Bootup::filterRequestInputs(); // Normalizes HTTP inputs to UTF-8 NFC
Run phpunit to see the code in action.
Make sure that you are confident about using UTF-8 by reading Character Sets / Character Encoding Issues and Handling UTF-8 with PHP, or PHP et UTF-8 for french readers.
You should also get familiar with the concept of Unicode Normalization and Grapheme Clusters.
Do not blindly replace all use of PHP's string functions. Most of the time you will not need to, and you will be introducing a significant performance overhead to your application.
Screen your input on the outer perimeter so that only well formed UTF-8 pass
through. When dealing with badly formed UTF-8, you should not try to fix it
(see Unicode Security Considerations).
Instead, consider it as CP-1252 and use
Patchwork\Utf8::utf8_encode() to get an UTF-8 string. Don't forget also to
choose one unicode normalization form and stick to it. NFC is now the defacto
standard. Patchwork\Utf8::filter() implements this behavior: it converts from
CP1252 and to NFC.
This library is orthogonal to mbstring.func_overload and will not work if the
php.ini setting is enabled.
Licensing
Patchwork\Utf8 is free software; you can redistribute it and/or modify it under the terms of the (at your option):
Unicode handling requires tedious work to be implemented and maintained on the long run. As such, contributions such as unit tests, bug reports, comments or patches licensed under both licenses are really welcomed.
I hope many projects could adopt this code and together help solve the unicode subject for PHP.
patchwork/utf8 适用场景与选型建议
patchwork/utf8 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.31M 次下载、GitHub Stars 达 627, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「i18n」 「utf8」 「utf-8」 「unicode」 「grapheme」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 patchwork/utf8 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 patchwork/utf8 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 patchwork/utf8 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use i18n translation PHP class for multi-language websites
A custom URL rule class for Yii 2 which allows to create translated URL rules
tfpdf
utf8 util
Removes invalid UTF-8 characters from the given text
Country Flag Emoji for PHP
统计信息
- 总下载量: 21.31M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 647
- 点击次数: 38
- 依赖项目数: 64
- 推荐数: 2
其他信息
- 授权协议: (Apache-2.0 or GPL-2.0) e1fa4d4a57896d074c9a8d01742b688d5db4e9d5
- 更新时间: 2026-01-04