php - Encoding SQL_Latin1_General_CP1_CI_AS into UTF-8 -


i'm generating xml file php using domdocument , need handle asian characters. i'm pulling data mssql2008 server using pdo_mssql driver , apply utf8_encode() on xml attribute values. works fine long there's no special characters.

the server ms sql server 2008 sp3

the database, table , column collation sql_latin1_general_cp1_ci_as

i'm using php 5.2.17

here's pdo object:

$pdo = new pdo("mssql:host=myserver,1433;dbname=mydatabase", user123, password123); 

my query basic select.

i know storing special characters sql_latin1_general_cp1_ci_as columns isn't great, ideally nice make work without changing it, because other non-php programs use column , works fine. in sql server management studio can see asian characters correctly.

considering details above, how should process data?

i found how solve it, helpful someone.

first, sql_latin1_general_cp1_ci_as strange mix of cp-1252 , utf-8. basic characters cp-1252, why had utf-8 , worked. asian , other utf-8 characters encoded on 2 bytes , php pdo_mssql driver seems hate varying length characters seems cast varchar (instead of nvarchar) , 2 byte characters become question marks ('?').

i fixed casting binary , rebuild text php:

select cast(my_column varbinary(max)) my_table; 

in php:

//binary hexadecimal $hex = bin2hex($bin);  //and hex string $str = ""; ($i=0;$i<strlen($hex) -1;$i+=2) {     $str .= chr(hexdec($hex[$i].$hex[$i+1])); } //and ucs-2le/sql_latin1_general_cp1_ci_as (that's column format in db) utf-8 $str = iconv('ucs-2le', 'utf-8', $str); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -