Str::ascii

Transliterates a UTF-8 string into its closest plain ASCII equivalent.

Signature
Str::ascii(string $value, string $language = 'en'): string

Parameters

ParameterTypeDescription
$$valuestringThe UTF-8 string to transliterate.
$$languageoptionalstring= 'en'Language code selecting a locale-specific transliteration table, for example 'de' so that ü becomes 'ue' rather than 'u'.

Return value

stringAn ASCII-only string with accented and non-Latin characters replaced by their closest ASCII equivalents. Characters with no known mapping are removed.

Errors

Nothing. Unmappable characters are dropped rather than raising an error.

Str::ascii delegates to voku/portable-ascii, a large lookup table rather than the ICU extension, so it produces the same output on every machine regardless of whether intl is installed. It is the engine that Str::slug() runs first, before it lowercases and hyphenates.

When to reach for it

Use it to build a normalised form for comparison or storage: a search key that lets Muller find Müller, an ASCII-safe filename, a sortable name column, or an email local part derived from a person's name. If you actually want a URL segment, call Str::slug() instead — it calls this for you and then handles separators and case.

What trips people up

It is lossy and silent. Anything the table cannot map is deleted, so a string of characters from an unsupported script can come back as an empty string with no warning. Never use it as a validator, and never store its output as the only copy of user-supplied text — keep the original and treat the ASCII form as a derived index.

The $language argument matters more than people expect. The default 'en' table maps ü to u; passing 'de' maps it to ue, which is what German speakers expect to type.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Build a search key so that typing "Bronte" matches a record stored as "Brontë".
  • Derive an ASCII-safe filename or S3 object key from a user-supplied title.
  • Normalise imported CSV names before comparing them against existing rows.

Frequently asked questions

How do I remove accents from a string in Laravel?

Str::ascii($value) transliterates accented characters to their closest ASCII equivalents, so 'Crème Brûlée' becomes 'Creme Brulee'. It needs no intl extension.

Does Str::ascii require the PHP intl extension?

No. It uses the voku/portable-ascii lookup tables that ship with illuminate/support, so output is identical across environments whether or not intl is installed.

Why did Str::ascii return an empty string?

Because none of the input characters had a mapping in the transliteration table, and unmappable characters are dropped rather than kept. Keep the original value and treat the ASCII form as a derived key.

More examples

Locale-aware transliteration

The same input produces different ASCII depending on the language table. If you build slugs for a German-language site, pass the language through to Str::slug($title, '-', 'de') so URLs match what readers would type.

Example · php
<?php

use Illuminate\Support\Str;

$name = 'Jürgen Größe';

echo Str::ascii($name), "\n";
// Jurgen Grosse   (default 'en' table)

echo Str::ascii($name, 'de'), "\n";
// Juergen Groesse (German table: ü => ue, ö => oe, ß => ss)

A normalised search column

Printing $index shows keys such as 'charlotte bronte' and 'gabriel garcia marquez' mapped back to the accented originals. An accent-free query now resolves to the correctly accented display name.

Example · php
<?php

use Illuminate\Support\Str;

$authors = ['Charlotte Brontë', 'Gabriel García Márquez', 'Émile Zola'];

$index = [];

foreach ($authors as $author) {
    // Keep the original; store the ASCII form only as a lookup key.
    $index[Str::lower(Str::ascii($author))] = $author;
}

print_r($index);

$query = Str::lower(Str::ascii('Gabriel Garcia Marquez'));

echo $index[$query] ?? 'not found', "\n";

Discussion

  • Be the first to comment on this lesson.