Protone Media logo

Convert accented characters with a Laravel helper function

Besides the famous Collection class that Laravel provides it also has a Str class that has lots of great functions to work with strings. Some of these functions are available through helper functions and others you have to find in the API documentation.

Last week we were working with a third party API that doesn't support accented characters as input values. I knew back in the CodeIgniter days there was a convert_accented_characters function and fortunately the Laravel String class offers something similar. The documentation describes the function as 'Transliterate a UTF-8 value to ASCII' and it does exactly what we wanted:

<?php

use Illuminate\Support\Str;
use PHPUnit\Framework\Assert;

$input = 'á é í ó ú ñ ü';
$converted = Str::ascii($input);

Assert::assertEquals('a e i o u n ue', $converted);

Related posts

Pascal Baljet on Twitter

Want to stay up-to-date on Laravel news and packages?

Follow me on Twitter: @pascalbaljet

© 2013-2020 Protone Media B.V.