踩坑教训

[踩坑] Base64_decode 导致 Malformed UTF-8 characters, possibly incorrectly encoded

By karp 1322 Views 3 MIN READ 0 Comments
$str = 'good, luck';
$content = base64_decode($str);
var_dump($content);
$content = $content ?: $str;

echo \json_encode($content), PHP_EOL;
echo json_last_error_msg(),PHP_EOL;

output...

$ string(6) ""

$ Malformed UTF-8 characters, possibly incorrectly encoded

问题如上, 特殊场景正常字符串使用 base64_decode 解码 字符串 异常解码 输出虽然是 “” 但实际上编码已发生改变.
修复上述问题.

# 二参数 strict 当设置 strict 为 true 时,一旦输入的数据超出了 base64 字母表,将返回 false。 否则会静默丢弃无效的字符。
$content = base64_decode($str, true);

上面的 base64_decode 二参数亲测不咋好用呀 'Victor here'

上面问题发生场景, 在于 内容老版本没加base64, 新版本有加 , 为了兼容, 产生的上面一系列问题.
还是写个脚本遍历修改下,老版本数据库记录比较省事.

本文由 karp 原创

采用 CC BY-NC-SA 4.0 协议进行许可

转载请注明出处:https://www.ikarp.top/index.php/archives/530.html

标签: php

0 评论