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;
}

文章来源:刘俊涛的博客 欢迎关注公众号、留言、评论,一起学习。


若有帮助到您,欢迎捐赠支持,您的支持是对我坚持最好的肯定(_)

posted @   刘俊涛的博客  阅读(3606)  评论(0编辑  收藏  举报
历史上的今天:
2017-06-30 分享到朋友圈遮罩层
Servbay
点击右上角即可分享
微信分享提示