Portable ASCII is a PHP library of static string functions for ASCII handling: transliteration, slugs, safe filenames and cleanup of invisible or malformed characters. Everything lives in the voku\helper\ASCII class. The package is webpatser/portable-ascii and has no runtime dependencies.
webpatser/portable-ascii is derived from the voku/portable-ascii project. It keeps the namespace, the 15 public methods of the original class and the Unicode-to-ASCII data tables, requires PHP 8.1 and fixes several transliteration tables. Existing code that uses the class runs unchanged, apart from the removed $remove_invalid_utf8 parameter of clean() (see the API Reference page).
This documentation covers 3.1.1.
Install
composer require webpatser/portable-ascii
Note: No tagged release is on Packagist yet; it lists only the
dev-mainbranch, which is the 3.1.1 code. Until a tag is published, require the branch explicitly:composer require webpatser/portable-ascii:dev-main.
The package's composer.json declares replace for the original package ("voku/portable-ascii": "2.0.99"), so Composer installs it in its place for every package that depends on it (Laravel's Str::ascii(), Str::slug() and Str::transliterate() among them). No code changes are needed.
Requirements
- PHP 8.1 or higher.
- No extensions are required. The class detects at runtime which extensions are loaded and uses the fastest available path.
ext-mbstringis suggested. When it is loaded,to_transliterate()splits characters withmb_str_split()instead ofpreg_match_all(), andclean()strips invalid UTF-8 withmb_convert_encoding()instead of a regex.ext-intlis suggested. It providestransliterator_transliterate()for the strict mode ofASCII::to_transliterate().
Use it
<?php use voku\helper\ASCII; echo ASCII::to_ascii('Düsseldorf', 'de'); // Duesseldorfecho ASCII::to_ascii('Düsseldorf', 'en'); // Dusseldorf
Continue with the Quick Start page.
Fledge variant
webpatser/fledge-portable-ascii (release v2.0.0) is a PHP 8.6 build of webpatser/portable-ascii for Fledge.
composer require webpatser/fledge-portable-ascii
What differs from webpatser/portable-ascii:
- It requires PHP
^8.6andext-mbstring. Both are mandatory. - It removes the runtime capability detection. It calls
mb_str_split()directly instead of falling back topreg_match_all(), and always usesmb_convert_encoding()for UTF-8 cleaning. - It has no
setCapabilities()method. ext-intlstays optional and is used for strict transliteration.- It supports only the latest stable PHP release. A new PHP release means a new package release that requires it.
- Its
composer.jsondeclaresreplaceentries for the original package and forwebpatser/portable-ascii, so it takes the place of both.
The other 15 public methods have the same signatures, and the namespace stays voku\helper\ASCII:
<?php use voku\helper\ASCII; ASCII::to_ascii('Düsseldorf', 'de'); // 'Duesseldorf'ASCII::is_ascii('hello'); // true