简体   繁体   English

UTF-8:成功转换为ISO-8859-1,但未成功转换为ISO-8859-2

[英]Utf-8: successful conversion to iso-8859-1 but not to iso-8859-2

I have a database on MS Access, that I use with PHP through a call with PDO and the odbc driver. 我在MS Access上有一个数据库,可以通过PDO和odbc驱动程序的调用与PHP一起使用。 I have French, Danish and Polish words in my database. 我的数据库中有法语,丹麦语和波兰语单词。 No problem for French and Danish, but no way to have the Polish characters, I only get "?" 对于法语和丹麦语来说没有问题,但没有波兰语字符的方式,我只会得到“?” instead. 代替。

Here is the code: 这是代码:

    try{
 $db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$dbName; Uid=Admin;Pwd=;");
  }
  catch(PDOException $e){
    echo $e->getMessage();
  }
  $answer = $db -> query("SELECT * FROM dict_main WHERE ID < 20");
      while($data = $answer-> fetch() ){
          echo iconv("iso-8859-1","utf-8",htmlspecialchars($data['DK'])) . ' ';
          echo iconv("iso-8859-2","utf-8",htmlspecialchars($data['PL'])) . ' ';
          echo iconv("iso-8859-1","utf-8",htmlspecialchars($data['FR'])) . ' ';
        }

Please let me know if somebody has an idea, as I am running out of them and nothing seems to work, or if I should give more information about my problem that I didn't think of. 请让我知道是否有人有想法,因为我已经快要精疲力尽了,似乎什么也没有用,或者我是否应该提供有关我未曾想到的问题的更多信息。

It looks like htmlspecialchars() does not support ISO-8859-2 . 看来htmlspecialchars()不支持ISO-8859-2 So it probably breaks the contents of $data['PL'] before it gets to iconv() . 因此,在到达iconv()之前,它可能会破坏$data['PL']的内容。

Try first converting the input string into UTF-8, then apply htmlspecialchars() to the UTF-8 string: 尝试首先将输入字符串转换为UTF-8,然后将htmlspecialchars()应用于UTF-8字符串:

echo htmlspecialchars( iconv("iso-8859-2", "utf-8", $data['PL']) );

You are using PHP 5.3.13. 您正在使用PHP 5.3.13。 Then i would expect the charset in new POD to do its job. 然后,我希望new POD的字符集可以完成其工作。 (Prior to 5.3.6. you would have to use $db->exec("set names utf8"); ). (在5.3.6之前。您必须使用$db->exec("set names utf8"); )。 So add the charset=utf8; 因此添加charset=utf8; to your connect line. 连接到您的连接线。 I also expect your Access database to be UTF-8. 我也希望您的Access数据库为UTF-8。

You can also try charset=ucs2; 您也可以尝试charset=ucs2; with and without htmlspecialchars( iconv("iso-8859-2", "utf-8", $data['PL']) ); 有和没有htmlspecialchars( iconv("iso-8859-2", "utf-8", $data['PL']) );

$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$dbName; Uid=Admin;Pwd=;charset=utf8;");

or 要么

$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$dbName; Uid=Admin;Pwd=;charset=ucs2;");

BTW : Don't forget to set your output to UTF-8 at the top of your document. 顺便说一句 :不要忘了在文档顶部将输出设置为UTF-8。

<?php header('Content-Type:text/html; charset=UTF-8'); ?>

and/or 和/或

<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>

If that still doesn't work i suspect that the encoding in your Access database is messed up. 如果那仍然不起作用,我怀疑您的Access数据库中的编码混乱了。


Edit: 编辑:

Only thing i can think of at this point is using odbc_connect directly and bypassing PDO but i think the problem is in ODBC (Access->ODBC). 在这一点上,我唯一能想到的就是直接使用odbc_connect并绕过PDO,但我认为问题出在ODBC(Access-> ODBC)中。 If that's the case this won't help: 如果是这种情况,这将无济于事:

$conn=odbc_connect("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$dbName; Uid=Admin;Pwd=;charset=utf8", "", "");
$rs=odbc_exec($conn, "SELECT * FROM dict_main WHERE ID < 20");
odbc_result_all($rs,"border=1");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM