Malformed UTF-8 characters, possibly incorrectly encoded
PHP 报错:
[ error ] [0]Malformed UTF-8 characters, possibly incorrectly encoded
原因:返回的内容用 UTF8 无法识别的内容;
解决:排查返回的相关内容,特别是 字符串截取,字符串替换的函数
例如:我在代码中使用了
substr_replace($str, str_repeat('*', $length), $start, $length);
substr_replace
替换中文时,无法截取;
优化后的:加密字符串为星号
/**
* 加密字符串为星号****
* 兼容 中文字符串
* @param string $str 字符串
* @param int $start 开始位置
* @param int $len 加密长度
* @return string
*/
function stringCryptic($str, $start = 4, $length = 4)
{
// return substr_replace($str, str_repeat('*', $length), $start, $length);
$startString = mb_substr($str, 0, $start, "UTF-8");
$endString = mb_substr($str, $start + $length, mb_strlen($str), "UTF-8");
$replacement = str_repeat('*', $length);
$out = $startString . $replacement . $endString;
return $out;
}
文章来源:刘俊涛的博客 欢迎关注公众号、留言、评论,一起学习。
若有帮助到您,欢迎捐赠支持,您的支持是对我坚持最好的肯定(_)
你要保守你心,胜过保守一切。
本文来自博客园,作者:刘俊涛的博客,转载请注明原文链接:https://www.cnblogs.com/lovebing/p/16427223.html