We've recently built Upserver.online, a sophisticated health checker for Laravel applications with support for databases, queues, broadcasting, mail, caching, security checks and it even monitors your uptime, DNS records, SSL certificates and mixed content. Create an account and follow us on Twitter!

Convert accented characters with a Laravel helper function

April 27, 2017

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:


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

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

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