Skip to content

Getting Started

Babel requires PHP 8.2+ with the following extensions:

  • ext-intl (ICU transliteration)
  • ext-mbstring (multibyte string handling)
  • ext-iconv (encoding conversion)

Install Babel with composer:

Terminal window
composer require cline/babel

Create a Babel instance from any string:

use Cline\Babel\Babel;
$babel = Babel::from('Héllo Wörld');

Chain methods for complex transformations:

$slug = Babel::from('Héllo Wörld!')
->toAscii(); // "Hello World!"

Babel handles null values gracefully:

$babel = Babel::from(null);
$babel->isEmpty(); // true
$babel->toAscii(); // null
$babel->isUtf8(); // true (empty is valid UTF-8)

All transformation methods return new instances:

$original = Babel::from('Café');
$ascii = $original->toAscii();
$original->value(); // "Café" (unchanged)
$ascii; // "Cafe"
Babel::from('Żółć')->toAscii(); // "Zolc"
Babel::from('北京')->toAscii(); // "bei jing"
Babel::from('Привет')->toAscii(); // "Privet"
Babel::from('Hello 世界')->containsChinese(); // true
Babel::from('Привет мир')->containsCyrillic(); // true
Babel::from('مرحبا')->isRtl(); // true
Babel::from("Hello\x00World")->removeNonPrintable()->value(); // "HelloWorld"
Babel::from('Hello 👋')->removeEmoji()->value(); // "Hello "
// Split into grapheme clusters
Babel::from('Hello')->graphemes(); // ['H', 'e', 'l', 'l', 'o']
Babel::from('café')->graphemes(); // ['c', 'a', 'f', 'é']
// Reverse preserving graphemes
Babel::from('café')->reverse()->value(); // "éfac"
Babel::from('Héllo Wörld!')->toSlug(); // "hello-world"