dtcyv3985 2015-06-29 01:33
浏览 141
已采纳

PHP从utf8_general_ci转换为latin1_swedish_ci

I'm receiving heaps of Data from a website and all of those String values need to be added in our database.

During the Inseration into the Database SQL throws sometimes following Error:

Warning:  PDOStatement::execute(): SQLSTATE[HY000]: General error: 1267 Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE)

The Database Tables are actually setted up to use Latin1.

After encoding my values with json_encode() I ve found out what occurs this Error. The UTF sequences, which are representing some specialcharackters, inbetween the strings need to be converted into their actual value:

encoded string: candidate\u00e2\u0080\u0099s individual circumstances

the sequence \u00e2\u0080\u0099 represents an ' in this example.

Whatever there are only a few different sequences and i also know the values i want/need to replace them with, but i am struggeling with the conversion.

I ve tried several aproaches but none of them worked out,

using str_replace:

str_replace('\\u00e2\\u0080\\u0099', '\'', ($string));

Didnt change anything in the string

using the mb_functions:

$encodedStr = mb_convert_encoding($string, 'ASCII')

left me with some misterious ?? instead of the UTF sequences but it doesnt throw the database error but it is still not what i need.

using preg_replace:

preg_replace('/\\u00e2\\u0080\\u0099/', '\'', $string)


threw an Error: PCRE does not support \L, \l, \N{name}, \U, or \u at offset 1

I ve tried several more options but those where the three ones which came up into my mind when i started forcing this problem and i just cant figure out why those functions especially str_replace doesnt work in the expected way.

  • 写回答

1条回答 默认 最新

  • douhuzhi0907 2015-06-29 23:44
    关注

    I finally solved the problem. Just incase somebody struggles with the same issue. The solution which worked for me was posted in

    I have a string with "\u00a0", and I need to replace it with "" str_replace fails

    private function convert($string) {
        /* Strings to remove:    
         *      \u00a0 = 
         *      \u00e2\u0080\u0099 = '
         *      
         */
        $string = str_replace(chr(194).chr(160), '', $string);  //removes \u00a0
        $string = str_replace('â', '', $string);  //removes \u00e2
        $string = str_replace(chr(194).chr(128).chr(194).chr(153), '\'', $string);  //removes \u0080\u0099
    
        return $string;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系