• <bdo id='A7uaf'></bdo><ul id='A7uaf'></ul>
    <tfoot id='A7uaf'></tfoot>

    <i id='A7uaf'><tr id='A7uaf'><dt id='A7uaf'><q id='A7uaf'><span id='A7uaf'><b id='A7uaf'><form id='A7uaf'><ins id='A7uaf'></ins><ul id='A7uaf'></ul><sub id='A7uaf'></sub></form><legend id='A7uaf'></legend><bdo id='A7uaf'><pre id='A7uaf'><center id='A7uaf'></center></pre></bdo></b><th id='A7uaf'></th></span></q></dt></tr></i><div id='A7uaf'><tfoot id='A7uaf'></tfoot><dl id='A7uaf'><fieldset id='A7uaf'></fieldset></dl></div>

    1. <legend id='A7uaf'><style id='A7uaf'><dir id='A7uaf'><q id='A7uaf'></q></dir></style></legend>

      <small id='A7uaf'></small><noframes id='A7uaf'>

        将 SQL_Latin1_General_CP1_CI_AS 编码为 UTF-8

        时间:2023-10-03
          <tbody id='VsuzR'></tbody>
        <legend id='VsuzR'><style id='VsuzR'><dir id='VsuzR'><q id='VsuzR'></q></dir></style></legend>

        <small id='VsuzR'></small><noframes id='VsuzR'>

        1. <i id='VsuzR'><tr id='VsuzR'><dt id='VsuzR'><q id='VsuzR'><span id='VsuzR'><b id='VsuzR'><form id='VsuzR'><ins id='VsuzR'></ins><ul id='VsuzR'></ul><sub id='VsuzR'></sub></form><legend id='VsuzR'></legend><bdo id='VsuzR'><pre id='VsuzR'><center id='VsuzR'></center></pre></bdo></b><th id='VsuzR'></th></span></q></dt></tr></i><div id='VsuzR'><tfoot id='VsuzR'></tfoot><dl id='VsuzR'><fieldset id='VsuzR'></fieldset></dl></div>

                  <bdo id='VsuzR'></bdo><ul id='VsuzR'></ul>

                  <tfoot id='VsuzR'></tfoot>

                • 本文介绍了将 SQL_Latin1_General_CP1_CI_AS 编码为 UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 DomDocument 用 PHP 生成 XML 文件,我需要处理亚洲字符.我正在使用 pdo_mssql 驱动程序从 MSSQL2008 服务器中提取数据,并对 XML 属性值应用 utf8_encode().只要没有特殊字符,一切正常.

                  I'm generating a XML file with PHP using DomDocument and I need to handle asian characters. I'm pulling data from the MSSQL2008 server using the pdo_mssql driver and I apply utf8_encode() on the XML attribute values. Everything works fine as long as there's no special characters.

                  服务器是 MS SQL Server 2008 SP3

                  The server is MS SQL Server 2008 SP3

                  数据库、表和列的排序规则都是SQL_Latin1_General_CP1_CI_AS

                  The database, table and column collation are all SQL_Latin1_General_CP1_CI_AS

                  我使用的是 PHP 5.2.17

                  I'm using PHP 5.2.17

                  这是我的 PDO 对象:

                  Here's my PDO object:

                  $pdo = new PDO("mssql:host=MyServer,1433;dbname=MyDatabase", user123, password123);
                  

                  我的查询是一个基本的 SELECT.

                  My query is a basic SELECT.

                  我知道将特殊字符存储到 SQL_Latin1_General_CP1_CI_AS 列中并不是很好,但理想情况下,让它在不更改的情况下工作会很好,因为其他非 PHP 程序已经使用该列并且它工作正常.在 SQL Server Management Studio 中,我可以正确地看到亚洲字符.

                  I know storing special characters into SQL_Latin1_General_CP1_CI_AS columns isn't great, but ideally it would be nice to make it work without changing it, because other non-PHP programs already use that column and it works fine. In SQL Server Management Studio I can see the asian characters correctly.

                  考虑到以上所有细节,我应该如何处理数据?

                  Considering all the details above, how should I process the data?

                  推荐答案

                  我找到了解决方法,所以希望这对某人有所帮助.

                  I found how to solve it, so hopefully this will be helpful to someone.

                  首先,SQL_Latin1_General_CP1_CI_AS 是 CP-1252 和 UTF-8 的奇怪组合.基本字符是 CP-1252,所以这就是为什么我所要做的就是 UTF-8 并且一切正常.亚洲和其他 UTF-8 字符以 2 个字节编码,php pdo_mssql 驱动程序似乎讨厌不同长度的字符,因此它似乎对 varchar(而不是 nvarchar)执行 CAST,然后所有 2 个字节字符都变成问号('?').

                  First, SQL_Latin1_General_CP1_CI_AS is a strange mix of CP-1252 and UTF-8. The basic characters are CP-1252, so this is why all I had to do was UTF-8 and everything worked. The asian and other UTF-8 characters are encoded on 2 bytes and the php pdo_mssql driver seems to hate varying length characters so it seems to do a CAST to varchar (instead of nvarchar) and then all the 2 byte characters become question marks ('?').

                  我通过将其转换为二进制文件来修复它,然后使用 php 重建文本:

                  I fixed it by casting it to binary and then I rebuild the text with php:

                  SELECT CAST(MY_COLUMN AS VARBINARY(MAX)) FROM MY_TABLE;
                  

                  在 php 中:

                  //Binary to hexadecimal
                  $hex = bin2hex($bin);
                  
                  //And then from hex to string
                  $str = "";
                  for ($i=0;$i<strlen($hex) -1;$i+=2)
                  {
                      $str .= chr(hexdec($hex[$i].$hex[$i+1]));
                  }
                  //And then from UCS-2LE/SQL_Latin1_General_CP1_CI_AS (that's the column format in the DB) to UTF-8
                  $str = iconv('UCS-2LE', 'UTF-8', $str);
                  

                  这篇关于将 SQL_Latin1_General_CP1_CI_AS 编码为 UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:file_exists() 和 file_get_contents() 在 PHP 中名为 outpu 下一篇:PHP 邮件编码主题行

                  相关文章

                  最新文章

                  1. <legend id='IURM6'><style id='IURM6'><dir id='IURM6'><q id='IURM6'></q></dir></style></legend>
                      <bdo id='IURM6'></bdo><ul id='IURM6'></ul>

                    <small id='IURM6'></small><noframes id='IURM6'>

                    <i id='IURM6'><tr id='IURM6'><dt id='IURM6'><q id='IURM6'><span id='IURM6'><b id='IURM6'><form id='IURM6'><ins id='IURM6'></ins><ul id='IURM6'></ul><sub id='IURM6'></sub></form><legend id='IURM6'></legend><bdo id='IURM6'><pre id='IURM6'><center id='IURM6'></center></pre></bdo></b><th id='IURM6'></th></span></q></dt></tr></i><div id='IURM6'><tfoot id='IURM6'></tfoot><dl id='IURM6'><fieldset id='IURM6'></fieldset></dl></div>

                    <tfoot id='IURM6'></tfoot>