18

Solving "Malformed UTF-8 characters, possibly incorrectly encoded" err...

 4 years ago
source link: https://barryvanveen.nl/blog/67-solving-malformed-utf-8-characters-possibly-incorrectly-encoded-error-in-laravel
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Solving "Malformed UTF-8 characters, possibly incorrectly encoded" error in Laravel

2 months ago, 1 comment, Barry van Veen

Here is a short blog for a problem I ran into this evening. I was testing a new endpoint that returns some user data in JSON format. Once every while the test was failing, it was flaky.

The error

The error was this one: Malformed UTF-8 characters, possibly incorrectly encoded at /path/to/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php:75.

The cause

Since the problem only happened once every couple of tests (and helped by the error message itself) I figured it must be related to the random user that I test with.

This turned out to be true. When a name contains some accented character, like "Ç", this error occured.

The solution

The solution turned out to be in my application code, it has nothing to do with Laravel itself.

The initials of the user are part of the return data. So in case of a user named "Isabella Özkan", those would be "IÖ".

The old code for getting the initials was using strtoupper and substr like this:

public function initials(): string
{
    return strtoupper(substr($this->first_name, 0, 1) . substr($this->last_name, 0, 1));
}

But these functions are not multi-byte safe and so they can give incorrect results for multi-byte characters. To solve this we should use the equivalent multi-byte safe functions:

public function initials(): string
{
    return mb_strtoupper(mb_substr($this->first_name, 0, 1) . mb_substr($this->last_name, 0, 1));
}

That should do it. By using the correct string functions you make sure all characters are properly handled. This, in turn, will allow everything to be outputted as a JSON response without problems.

Happy coding!

Read some more

Comments

1
Philipp 2020-11-16 13:14:04

Add your comment

Your name
Email address (not visible to others)
Your message

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK