0

I'm trimming the first_name and the last_name of my users on a Model via Eloquent Accessors with the next helper:

function clean($string){
    return trim($string, " \t\n\r\0\x0B\xc2\xa0");
}

Then in the model:

public function getFirstNameAttribute($firstname){
    return clean(ucwords(strtolower($firstname)));
}
public function getLastNameAttribute($lastname){
    return clean(ucwords(strtolower($lastname)));
}

A lot of names comes from an excel an has trailing spaces which can be removed trimming \xa0, but the problem comes when it tries to trim special characters, as (à).

I tried removing it and it doesn't fail, but well, it doesn't remove the trailing spaces.

I also tried making sure that the headers charset were set to utf-8

Any idea? Thanks.

I just want the invisible spaces to be gone :/

The error message:

InvalidArgumentException in JsonResponse.php line 69:
Malformed UTF-8 characters, possibly incorrectly encoded
Cœur
  • 37,241
  • 25
  • 195
  • 267
Carlos Fdev
  • 745
  • 6
  • 24

1 Answers1

0

Try to use mb_strtolower() and mb_convert_case() instead of strtolower() and ucwords().

Instead of trim() you could try this function from here:

function mb_trim($str) {
    return preg_replace("/(^\s+)|(\s+$)/us", "", $str); 
}
Community
  • 1
  • 1
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • Thank you for the tip, tried, but it doesn't work either, it is the trim. – Carlos Fdev Nov 08 '16 at 15:32
  • tried too, it doesn't remove all the trailing spaces { "id": 95063, "first_name": "Xabier "} only when I add \xa0 to the trim it actually remove them all – Carlos Fdev Nov 08 '16 at 16:11