gpt4 book ai didi

php - 为mysql准备UTF-8字符串

转载 作者:行者123 更新时间:2023-11-29 21:45:55 25 4
gpt4 key购买 nike

我从电子邮件主题中获取了一个字符串

输入

=?UTF-8?B?RndkOiDwn5G+IEZpbmFsIEhvdXJzIHRvIFNhdmUg8J+Rvg==?=

输出(来自convert_nin_acsii方法)

Fwd: 👾 Final Hours to Save 👾

我想将其插入到mysql中,但出现错误

错误

2015-12-06 11:11 SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xF0\x9F\x91\xBE F...' for column 'label' at ...

代码

我使用这段代码为mysql准备字符串

private function convert_non_ascii($string){
$return = '';
if(preg_match('/^=\?(iso-8859-1|utf-8)\?q\?/i', $string)){
$return = str_replace('_',' ', mb_decode_mimeheader($string));
}
elseif(preg_match('/^(iso-8859-1\'\')(.*)$/i', $string, $matches)){
$return = utf8_encode(rawurldecode($matches[2]));
}
else{
$return = imap_utf8($string);
}

// Fix: Remove all non UTF-8 characters if mail is not correctly encoded
$return = mb_convert_encoding($return, 'UTF-8', 'UTF-8');

return $return;
}

最佳答案

MySQL 中的列/表必须声明为字符集 utf8mb4。 MySQL 的 utf8 不足以支持 Emoji。

并且您需要设置名称 utf8mb4

👾 的 utf8 十六进制为 F09F91BE(4 个字节),可以在错误消息中看到。

关于php - 为mysql准备UTF-8字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34116807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com