dougudu3564 2014-02-18 14:46
浏览 23
已采纳

PHP错误的字符集

I am trying to pull data from a table and output it as a text (RTF) file. The problem is that there are some characters in the content that get mangled. For instance, if I have Spanish content, some of the characters are not recognized and get changed. For example, if I have:

'implementación'

the word gets changed to:

'implementación'

By using break points, I can see that the string coming from the database is correct, it's only when it gets printed out that the tilde get's changed. Below is my code:

           header("Content-Type: application/rtf; charset=utf-8;");
           header("Cache-Control: public");
           header("Content-Description: File Transfer");
           header("Content-Disposition: attachment; filename=".$fileName .".rtf");
           header("Content-Transfer-Encoding: binary");

           echo $content;

Thanks for your help.

jason

  • 写回答

2条回答 默认 最新

  • douzi7219 2014-02-18 14:50
    关注

    Match the output character set with the table's character set or convert the character set from the table with the character set you want to output.

    Assuming the table uses US-ASCII to store data and we want to output it as UTF-8.

    $content = iconv( 'US-ASCII', 'UTF-8//IGNORE//TRANSLIT', $content );
    echo $content;
    

    This will transliterate certain characters EG: € to EUR, and ignore/drop characters that are not known to the output character set.

    If you are using Latin-1-General encoding in the table try CP850 (AKA: Code Page 850, MSDOS Latin-1) as opposed to US-ASCII.

    http://us2.php.net/manual/en/function.iconv.php

    You can optionally cast your encoding from within your query to the table For example with mysql

    SELECT convert(cast(convert(content using  latin1) as binary) using utf8) AS content
    

    MySQL - Convert latin1 characters on a UTF8 table into UTF8

    This is useful if the data sent to the database was using a different character set than the table. For example sending ASCII or ISO-8859-1 data to a table/column using UTF-8 collation.

    To find out the tables character encoding try:

    SHOW CREATE TABLE `tablename`;
    

    or How do I see what character set a MySQL database / table / column is?

    For table encoding:

    SELECT CCSA.character_set_name FROM information_schema.`TABLES` T,
           information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
    WHERE CCSA.collation_name = T.table_collation
      AND T.table_schema = "schemaname"
      AND T.table_name = "tablename";
    

    For column encoding:

    SELECT character_set_name FROM information_schema.`COLUMNS` C
    WHERE table_schema = "schemaname"
      AND table_name = "tablename"
      AND column_name = "columnname";
    

    Alternatively you can try changing the charset header in PHP to match the database table's output.

    header("Content-Type: application/rtf; charset=ISO-8859-1;");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?