~/docs
Resonate
Resonate
3.x 3.1.1, current
Portable ASCII
Toggle sidebar
Portable ASCII
webpatser/portable-ascii 3.1.1

Portable ASCII 3.x

Examples

Complete examples for transliteration, slugs, filenames, text cleanup and character maps.

Language-specific conversion

The language argument of to_ascii() selects the mapping. The same input gives different output per language.

<?php
 
use voku\helper\ASCII;
 
echo ASCII::to_ascii('Düsseldorf', 'de'); // Duesseldorf
echo ASCII::to_ascii('Düsseldorf', 'en'); // Dusseldorf

Extra symbols

With $replace_extra_symbols set to true, symbols such as £ are spelled out. The signature is to_ascii($str, $language, $remove_unsupported_chars, $replace_extra_symbols, ...).

<?php
 
use voku\helper\ASCII;
 
echo ASCII::to_ascii('Price: £5', 'en', true, true); // "Price: pound 5"
echo ASCII::to_slugify('test © test', '-', 'en', [], true); // test-c-test

Slugs with a custom separator and replacements

<?php
 
use voku\helper\ASCII;
 
echo ASCII::to_slugify('Hello World', '_'); // hello_world
echo ASCII::to_slugify('Hello World', '.'); // hello.world
echo ASCII::to_slugify('CamelCase', '_', 'en', [], false, false); // Camel_Case
echo ASCII::to_slugify('Tom & Jerry', '-', 'en', ['&' => 'and']); // tom-and-jerry

Transliteration with unknown characters

<?php
 
use voku\helper\ASCII;
 
echo ASCII::to_transliterate('déjà σσς iıii'); // deja sss iiii
echo ASCII::to_transliterate('こんにちは'); // konnichiha

Clean user input

clean() removes malformed UTF-8 and, by default, normalizes whitespace and MS Word characters and removes invisible characters.

<?php
 
use voku\helper\ASCII;
 
$input = "a\xC2\xA0b…\xff"; // non-breaking space, ellipsis, invalid byte
 
echo ASCII::clean($input); // "a b..."
echo ASCII::normalize_msword('„Abcdef…”'); // '"Abcdef..."'
echo ASCII::remove_invisible_characters("Java\0script"); // Javascript

Look up the character maps

<?php
 
use voku\helper\ASCII;
 
$array = ASCII::charsArray();
var_dump($array['ru']['б']); // 'b'
 
$multi = ASCII::charsArrayWithMultiLanguageValues();
var_dump($multi['b']); // ['б', 'բ', 'ဗ', 'ბ', 'ب', 'ব']
 
$languages = ASCII::getAllLanguages(); // ['uzbek' => 'uz', 'turkmen' => 'tk', ...]