简体   繁体   English

在雄辩的访问器中修整(\\ xa0)后出现UTF-8错误

[英]UTF-8 Error after trim(\xa0) in Eloquent accessor

I'm trimming the first_name and the last_name of my users on a Model via Eloquent Accessors with the next helper: 我正在通过Eloquent Accessors和下一个帮助器在模型上修剪用户的名字和姓氏:

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 (à). 许多名称都来自excel,并且具有尾随空格,可以将其修剪\\ xa0删除,但是当它试图修剪特殊字符时,问题就来了,例如(à)。

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 我还尝试确保将标头字符集设置为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

Try to use mb_strtolower() and mb_convert_case() instead of strtolower() and ucwords() . 尝试使用mb_strtolower()mb_convert_case()代替strtolower()ucwords()

Instead of trim() you could try this function from here : 代替trim()您可以从这里尝试此功能:

function mb_trim($str) {
    return preg_replace("/(^\s+)|(\s+$)/us", "", $str); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM