为mysql

时间:2015-12-06 11:17:02

标签: php mysql utf-8

我收到了电子邮件主题的字符串

输入

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

1 个答案:

答案 0 :(得分:0)

MySQL中的列/表必须声明为SET NAMES utf8mb4。 MySQL的utf8对于Emoji来说是不够的。

您需要

F09F91BE的utf8十六进制为public class Product { [PrimaryKey, AutoIncrement] public int ChemID { get; set; } public string ChemCat { get; set; } public string ChemName { get; set; } public double ChemWeight { get; set; } } (4个字节),可以在错误消息中看到。