All functions are static methods on voku\helper\ASCII. webpatser/portable-ascii keeps that namespace, so import the class once:
<?php use voku\helper\ASCII;
Try it
Type some text and pick a language. The four calls below run on this server as you type.
ASCII::to_ascii($text, 'en')- 'Hello World! Dusseldorf, Privet mir, deja vu.'
ASCII::to_slugify($text, '-', 'en')- 'hello-world-dusseldorf-privet-mir-deja-vu'
ASCII::to_filename($text)- 'Hello-World-Dusseldorf-Privet-mir-deja-vu.'
ASCII::is_ascii($text)- false
This site runs webpatser/fledge-portable-ascii 2.0.0, the PHP 8.6 variant of webpatser/portable-ascii. It ships the same voku\helper\ASCII class, and the four methods shown here have the same signatures in both.
Check for ASCII
ASCII::is_ascii('白'); // falseASCII::is_ascii('abc'); // true
Convert to ASCII
to_ascii() replaces non-ASCII characters with their closest ASCII counterparts. The second argument is the language of the source string. It changes the mapping: German turns ü into ue, English turns it into u.
ASCII::to_ascii('Düsseldorf', 'de'); // 'Duesseldorf'ASCII::to_ascii('Düsseldorf', 'en'); // 'Dusseldorf'ASCII::to_ascii('Привет мир', 'ru'); // 'Privet mir'
The language accepts the forms en, en_GB and en-GB. The class has constants for the supported languages, such as ASCII::GERMAN_LANGUAGE_CODE ('de'). ASCII::getAllLanguages() returns them all.
By default, characters without a mapping are removed. Pass false as the third argument ($remove_unsupported_chars) to keep them.
ASCII::to_ascii('Hi 白', 'en'); // 'Hi 'ASCII::to_ascii('Hi 白', 'en', false); // 'Hi 白'
Transliterate
to_transliterate() converts to ASCII without a language argument.
ASCII::to_transliterate('déjà σσς iıii'); // 'deja sss iiii'ASCII::to_transliterate('こんにちは'); // 'konnichiha'
The second argument (?string $unknown, default '?') is the replacement for characters without a mapping; null keeps them. With ext-intl installed, a third argument of true uses transliterator_transliterate().
Create a slug
ASCII::to_slugify('Hello Wörld!'); // 'hello-world'ASCII::to_slugify('Hello Wörld!', '-', 'de'); // 'hello-woerld'ASCII::to_slugify('Hello World', '_'); // 'hello_world'ASCII::to_slugify('CamelCase', '_', 'en', [], false, false); // 'Camel_Case'
The second argument is the separator, the third the language, the fourth a map of replacements, and the sixth ($use_str_to_lower) turns lowercasing off.
Create a safe filename
ASCII::to_filename('My Report: final?.pdf'); // 'My-Report-final.pdf'
The case is kept. Unsafe characters are removed, whitespace becomes the fallback character (a hyphen by default), and characters that transliteration cannot map also become the fallback character.
Clean up
ASCII::normalize_msword('„Abcdef…”'); // '"Abcdef..."'ASCII::remove_invisible_characters("Java\0script"); // 'Javascript'
See the API Reference page for every method and parameter, and the Examples page for longer snippets.