All methods are public static on voku\helper\ASCII. webpatser/portable-ascii keeps that namespace for drop-in compatibility. Signatures below are taken from the 3.1.1 code.
Compared with the original 2.x class, two signatures changed: clean() no longer has a $remove_invalid_utf8 parameter (invalid UTF-8 is always stripped), and the $unknown parameter of to_transliterate() is typed ?string. setCapabilities() is new.
Language constants
Each supported language has a constant named *_LANGUAGE_CODE, for example ASCII::GERMAN_LANGUAGE_CODE ('de'), ASCII::ENGLISH_LANGUAGE_CODE ('en') and ASCII::RUSSIAN_LANGUAGE_CODE ('ru'). Variants such as de_at, de_ch, fr_at, fr_ch, sr__cyr and sr__lat have their own constants. Methods that take a $language accept the forms en, en_GB and en-GB.
getAllLanguages
ASCII::getAllLanguages(): array
Returns an associative array of every *_LANGUAGE_CODE constant: the key is the lowercased constant name without _LANGUAGE_CODE (for example 'german' => 'de'), and the EXTRA_* codes (latin, whitespace, msword) are included too.
setCapabilities
ASCII::setCapabilities(?array $caps): void
Overrides the runtime extension detection. The source marks it as meant for benchmarking and testing only. Pass an array with the keys php (int), mbstring (bool) and intl (bool); a missing mbstring or intl key counts as false, a missing php key uses PHP_VERSION_ID. Pass null to return to automatic detection.
ASCII::setCapabilities(['mbstring' => false]); // force the preg_match_all() pathASCII::setCapabilities(null); // detect again
is_ascii
ASCII::is_ascii(string $str): bool
Returns true if the string is 7 bit ASCII.
ASCII::is_ascii('白'); // false
to_ascii
ASCII::to_ascii( string $str, string $language = 'en', bool $remove_unsupported_chars = true, bool $replace_extra_symbols = false, bool $use_transliterate = false, bool $replace_single_chars_only = false): string
Returns an ASCII version of the string. Non-ASCII characters are replaced with their closest ASCII counterparts, and the rest are removed by default.
| Parameter | Meaning |
|---|---|
$str |
The input string. |
$language |
Language of the source string. Changes the mapping, for example de maps ä, ö, ü to ae, oe, ue. |
$remove_unsupported_chars |
Remove characters without a mapping. |
$replace_extra_symbols |
Add more replacements, for example £ with pound. |
$use_transliterate |
Use ASCII::to_transliterate() for unknown characters. |
$replace_single_chars_only |
Single character replacement is better for performance, but some languages need to replace more than one character at a time. |
ASCII::to_ascii('Düsseldorf', 'en'); // 'Dusseldorf'
to_transliterate
ASCII::to_transliterate(string $str, ?string $unknown = '?', bool $strict = false): string
Returns an ASCII version of the string. Non-ASCII characters are replaced with their closest ASCII counterparts.
| Parameter | Meaning |
|---|---|
$unknown |
Replacement for valid characters without a mapping (default '?'). null keeps them. Malformed UTF-8 is discarded during cleaning. |
$strict |
Use transliterator_transliterate() from ext-intl. |
ASCII::to_transliterate('déjà σσς iıii'); // 'deja sss iiii'
to_slugify
ASCII::to_slugify( string $str, string $separator = '-', string $language = 'en', array $replacements = [], bool $replace_extra_symbols = false, bool $use_str_to_lower = true, bool $use_transliterate = false): string
Converts the string into a URL slug: non-ASCII characters are replaced with their closest ASCII equivalents, remaining non-ASCII and non-alphanumeric characters are removed, and whitespace is replaced with $separator. The string is lowercased unless $use_str_to_lower is false. $replacements is a map of strings to replace.
ASCII::to_slugify('Hello World', '_'); // 'hello_world'
to_filename
ASCII::to_filename(string $str, bool $use_transliterate = true, string $fallback_char = '-'): string
Converts the string to a safe filename and keeps the case. With $use_transliterate set to true, to_transliterate() runs first and unmapped characters become $fallback_char. In both modes remaining unsafe characters are removed, whitespace becomes $fallback_char, repeated fallback characters collapse to one and leading/trailing ones are trimmed.
clean
ASCII::clean( string $str, bool $normalize_whitespace = true, bool $keep_non_breaking_space = false, bool $normalize_msword = true, bool $remove_invisible_characters = true): string
Always strips invalid UTF-8 byte sequences (with mb_convert_encoding() when ext-mbstring is loaded, otherwise with a regex), then applies whitespace normalization, MS Word character normalization and invisible character removal when enabled. $keep_non_breaking_space works together with $normalize_whitespace.
normalize_msword
ASCII::normalize_msword(string $str): string
Replaces smart quotes, ellipsis characters and dashes from Windows-1252 (common in Word documents) with their ASCII equivalents.
ASCII::normalize_msword('„Abcdef…”'); // '"Abcdef..."'
normalize_whitespace
ASCII::normalize_whitespace( string $str, bool $keepNonBreakingSpace = false, bool $keepBidiUnicodeControls = false, bool $normalize_control_characters = false): string
Normalizes whitespace. $keepNonBreakingSpace keeps non-breaking spaces. $keepBidiUnicodeControls keeps non-printable bidirectional text characters. $normalize_control_characters converts line and paragraph separators to "\n" and line tabulation to "\t".
remove_invisible_characters
ASCII::remove_invisible_characters( string $str, bool $url_encoded = false, string $replacement = '', bool $keep_basic_control_characters = true): string
Removes invisible characters, which prevents sandwiching null characters between ASCII characters such as Java\0script. The source notes that it is copied from CodeIgniter's Common.php.
to_ascii_remap
ASCII::to_ascii_remap(string $str1, string $str2): array
Warning: This method returns broken characters and is only for special cases.
Converts two UTF-8 strings to single-byte strings for functions that need the same string length after conversion. Non-ASCII characters are remapped to the range 128 to 255 in order of appearance. Returns a string[].
charsArray
ASCII::charsArray(bool $replace_extra_symbols = false): array
Returns the replacement array, keyed by language.
$array = ASCII::charsArray();var_dump($array['ru']['б']); // 'b'
charsArrayWithMultiLanguageValues
ASCII::charsArrayWithMultiLanguageValues(bool $replace_extra_symbols = false): array
Returns the replacement array with a mix of multiple languages: each ASCII replacement maps to the characters that produce it.
charsArrayWithOneLanguage
ASCII::charsArrayWithOneLanguage( string $language = 'en', bool $replace_extra_symbols = false, bool $asOrigReplaceArray = true): array
Returns the replacement array for one language. German maps ä to ae, other languages map it to a. With $asOrigReplaceArray set to true the result has the shape ['orig' => string[], 'replace' => string[]].
$array = ASCII::charsArrayWithOneLanguage('ru');$tmpKey = \array_search('yo', $array['replace']);echo $array['orig'][$tmpKey]; // 'ё'
charsArrayWithSingleLanguageValues
ASCII::charsArrayWithSingleLanguageValues( bool $replace_extra_symbols = false, bool $asOrigReplaceArray = true): array
Returns the replacement array where each character has a single language value. With $asOrigReplaceArray set to true the result has the shape ['orig' => string[], 'replace' => string[]].