zbateson / mb-wrapper
Wrapper for mbstring with fallback to iconv for encoding conversion and string manipulation
Fund package maintenance!
zbateson
Installs: 46 876 205
Dependents: 4
Suggesters: 0
Security: 0
Stars: 49
Watchers: 2
Forks: 8
Open Issues: 2
pkg:composer/zbateson/mb-wrapper
Requires
- php: >=8.1
- symfony/polyfill-iconv: ^1.9
- symfony/polyfill-mbstring: ^1.9
Requires (Dev)
- friendsofphp/php-cs-fixer: *
- phpstan/phpstan: *
- phpunit/phpunit: ^10.0|^11.0
Suggests
- ext-iconv: For best support/performance
- ext-mbstring: For best support/performance
README
Charset conversion and string manipulation wrapper with a large defined set of aliases.
composer require zbateson/mb-wrapper
Sponsors
A huge thank you to all my sponsors. <3
If this project's helped you, please consider sponsoring me.
Requirements
PHP 8.1 or newer. Tested on PHP 8.1, 8.2, 8.3, 8.4, and 8.5.
Description
MbWrapper is intended for use wherever mb_* or iconv_* is used. It scans supported charsets returned by mb_list_encodings(), and prefers mb_* functions, but will fallback to iconv if a charset isn't supported by the mb_* functions.
A list of aliased charsets is maintained for both mb_* and iconv, where a supported charset exists for an alias. This is useful for mail and http parsing as other systems may report encodings not recognized by mb_* or iconv.
Charset lookup is done by removing non-alphanumeric characters as well, so UTF8 will always be matched to UTF-8, etc.
Usage
The following wrapper methods are exposed:
mb_convert_encoding,iconvwithMbWrapper::convertmb_substr,iconv_substrwithMbWrapper::getSubstrmb_strlen,iconv_strlenwithMbWrapper::getLengthmb_check_encoding,iconv(for verification) withMbWrapper::checkEncoding
$mbWrapper = new \ZBateson\MbWrapper\MbWrapper(); $fromCharset = 'ISO-8859-1'; $toCharset = 'UTF-8'; $mbWrapper->convert('data', $fromCharset, $toCharset); $mbWrapper->getLength('data', 'UTF-8'); $mbWrapper->substr('data', 'UTF-8', 1, 2); if ($mbWrapper->checkEncoding('data', 'UTF-8')) { echo 'Compatible'; }
License
BSD licensed - please see license agreement.