How to properly convert characters in GB2312 to UTF-8 in PHP?

1k Views Asked by At

I have variables with chinese words, their charset is GB2312. I want to convert them to UTF-8 because I want to save them to mysql table with utf-8 encoding. How to do that is PHP? I'm using PHP 7.

Here are what I have tried:

  1. I have tried using $myvar = iconv('gb2312', 'utf-8', $myvar); However some of my variables get empty if it contains some characters (invalid UTF-8 chars maybe?)
  2. I have tried using $myvar = mb_convert_encoding($myvar, 'UTF-8', 'GB2312'); It works better than iconv but when $myvar contain some characters as I mentioned above, they turned into question mark (?)

Please help me, thanks

Update Here is an example of my chinese string:

  • GB2312 (Expected result): 第3章︰林鴻
  • Using mb_convert_encoding become: 第3章?林?
  • Using iconv become empty
0

There are 0 best solutions below