Когда я пытался получить китайские символы из базы данных, я получил странный текст. Я пробовал почти все, например html_entity_decode
, htmlentities
, сохранить файл с помощью utf-8, закодировать в utf-8, но я не могу понять, что это правильно.
Как получить правильный текст?
Вот мой код:
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> <?php header('Content-Type: text/html; charset=utf-8'); $conn=odbc_connect('vocab','',''); $rs1=odbc_exec($conn,"SELECT MAX(ID) AS MaxId FROM vocab"); $NewMaxID=odbc_result($rs1,"MaxId"); $rand=rand(1,$NewMaxID); $sql="SELECT word,part_of_speech,chinese FROM vocab WHERE ID=".$rand.";"; $rs=odbc_exec($conn,$sql); $i=1; odbc_fetch_row($rs); $a=(odbc_result($rs,1)); $b=(odbc_result($rs,2)); $c=(odbc_result($rs,3)); //$c="鎮"; //$d=html_entity_decode($c); //$c=htmlentities($d, ENT_NOQUOTES , "UTF-8"); $rows=array("first"=>$a,"second"=>$b,"third"=>$c); echo json_encode($rows); ?>
ps: Я использую традиционную китайскую версию MS Office.
Я столкнулся с этой проблемой некоторое время назад, и единственный способ заставить ее работать – написать HTML в объект ADODB.Stream
, сохранить его в файл и затем echo
-файл:
<?php define("TEMP_FOLDER", "C:\\__tmp\\"); header('Content-Type: text/html; charset=utf-8'); $stm = new COM("ADODB.Stream") or die("Cannot create COM object."); $stm->Type = 2; // adTypeText $stm->Charset = 'utf-8'; $stm->Open(); $stm->WriteText('<html>'); $stm->WriteText('<head>'); $stm->WriteText('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'); $stm->WriteText('<title>ADODB test</title>'); $stm->WriteText('</head>'); $stm->WriteText('<body>'); $con = new COM("ADODB.Connection"); $con->Open( "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" . "Dbq=C:\\Users\\Public\\Database1.accdb"); $rst = $con->Execute("SELECT word FROM vocab WHERE ID=3"); $stm->WriteText($rst->Fields("word")); $rst->Close(); $con->Close(); $stm->WriteText('</body>'); $stm->WriteText('</html>'); $tempFile = TEMP_FOLDER . uniqid("", TRUE) . ".txt"; $stm->SaveToFile($tempFile, 2); // adSaveCreateOverWrite $stm->Close(); echo file_get_contents($tempFile); unlink($tempFile); ?>
-<?php define("TEMP_FOLDER", "C:\\__tmp\\"); header('Content-Type: text/html; charset=utf-8'); $stm = new COM("ADODB.Stream") or die("Cannot create COM object."); $stm->Type = 2; // adTypeText $stm->Charset = 'utf-8'; $stm->Open(); $stm->WriteText('<html>'); $stm->WriteText('<head>'); $stm->WriteText('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'); $stm->WriteText('<title>ADODB test</title>'); $stm->WriteText('</head>'); $stm->WriteText('<body>'); $con = new COM("ADODB.Connection"); $con->Open( "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" . "Dbq=C:\\Users\\Public\\Database1.accdb"); $rst = $con->Execute("SELECT word FROM vocab WHERE ID=3"); $stm->WriteText($rst->Fields("word")); $rst->Close(); $con->Close(); $stm->WriteText('</body>'); $stm->WriteText('</html>'); $tempFile = TEMP_FOLDER . uniqid("", TRUE) . ".txt"; $stm->SaveToFile($tempFile, 2); // adSaveCreateOverWrite $stm->Close(); echo file_get_contents($tempFile); unlink($tempFile); ?>
-<?php define("TEMP_FOLDER", "C:\\__tmp\\"); header('Content-Type: text/html; charset=utf-8'); $stm = new COM("ADODB.Stream") or die("Cannot create COM object."); $stm->Type = 2; // adTypeText $stm->Charset = 'utf-8'; $stm->Open(); $stm->WriteText('<html>'); $stm->WriteText('<head>'); $stm->WriteText('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'); $stm->WriteText('<title>ADODB test</title>'); $stm->WriteText('</head>'); $stm->WriteText('<body>'); $con = new COM("ADODB.Connection"); $con->Open( "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" . "Dbq=C:\\Users\\Public\\Database1.accdb"); $rst = $con->Execute("SELECT word FROM vocab WHERE ID=3"); $stm->WriteText($rst->Fields("word")); $rst->Close(); $con->Close(); $stm->WriteText('</body>'); $stm->WriteText('</html>'); $tempFile = TEMP_FOLDER . uniqid("", TRUE) . ".txt"; $stm->SaveToFile($tempFile, 2); // adSaveCreateOverWrite $stm->Close(); echo file_get_contents($tempFile); unlink($tempFile); ?>
-<?php define("TEMP_FOLDER", "C:\\__tmp\\"); header('Content-Type: text/html; charset=utf-8'); $stm = new COM("ADODB.Stream") or die("Cannot create COM object."); $stm->Type = 2; // adTypeText $stm->Charset = 'utf-8'; $stm->Open(); $stm->WriteText('<html>'); $stm->WriteText('<head>'); $stm->WriteText('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'); $stm->WriteText('<title>ADODB test</title>'); $stm->WriteText('</head>'); $stm->WriteText('<body>'); $con = new COM("ADODB.Connection"); $con->Open( "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" . "Dbq=C:\\Users\\Public\\Database1.accdb"); $rst = $con->Execute("SELECT word FROM vocab WHERE ID=3"); $stm->WriteText($rst->Fields("word")); $rst->Close(); $con->Close(); $stm->WriteText('</body>'); $stm->WriteText('</html>'); $tempFile = TEMP_FOLDER . uniqid("", TRUE) . ".txt"; $stm->SaveToFile($tempFile, 2); // adSaveCreateOverWrite $stm->Close(); echo file_get_contents($tempFile); unlink($tempFile); ?>
-<?php define("TEMP_FOLDER", "C:\\__tmp\\"); header('Content-Type: text/html; charset=utf-8'); $stm = new COM("ADODB.Stream") or die("Cannot create COM object."); $stm->Type = 2; // adTypeText $stm->Charset = 'utf-8'; $stm->Open(); $stm->WriteText('<html>'); $stm->WriteText('<head>'); $stm->WriteText('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'); $stm->WriteText('<title>ADODB test</title>'); $stm->WriteText('</head>'); $stm->WriteText('<body>'); $con = new COM("ADODB.Connection"); $con->Open( "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" . "Dbq=C:\\Users\\Public\\Database1.accdb"); $rst = $con->Execute("SELECT word FROM vocab WHERE ID=3"); $stm->WriteText($rst->Fields("word")); $rst->Close(); $con->Close(); $stm->WriteText('</body>'); $stm->WriteText('</html>'); $tempFile = TEMP_FOLDER . uniqid("", TRUE) . ".txt"; $stm->SaveToFile($tempFile, 2); // adSaveCreateOverWrite $stm->Close(); echo file_get_contents($tempFile); unlink($tempFile); ?>
-<?php define("TEMP_FOLDER", "C:\\__tmp\\"); header('Content-Type: text/html; charset=utf-8'); $stm = new COM("ADODB.Stream") or die("Cannot create COM object."); $stm->Type = 2; // adTypeText $stm->Charset = 'utf-8'; $stm->Open(); $stm->WriteText('<html>'); $stm->WriteText('<head>'); $stm->WriteText('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'); $stm->WriteText('<title>ADODB test</title>'); $stm->WriteText('</head>'); $stm->WriteText('<body>'); $con = new COM("ADODB.Connection"); $con->Open( "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" . "Dbq=C:\\Users\\Public\\Database1.accdb"); $rst = $con->Execute("SELECT word FROM vocab WHERE ID=3"); $stm->WriteText($rst->Fields("word")); $rst->Close(); $con->Close(); $stm->WriteText('</body>'); $stm->WriteText('</html>'); $tempFile = TEMP_FOLDER . uniqid("", TRUE) . ".txt"; $stm->SaveToFile($tempFile, 2); // adSaveCreateOverWrite $stm->Close(); echo file_get_contents($tempFile); unlink($tempFile); ?>
-<?php define("TEMP_FOLDER", "C:\\__tmp\\"); header('Content-Type: text/html; charset=utf-8'); $stm = new COM("ADODB.Stream") or die("Cannot create COM object."); $stm->Type = 2; // adTypeText $stm->Charset = 'utf-8'; $stm->Open(); $stm->WriteText('<html>'); $stm->WriteText('<head>'); $stm->WriteText('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'); $stm->WriteText('<title>ADODB test</title>'); $stm->WriteText('</head>'); $stm->WriteText('<body>'); $con = new COM("ADODB.Connection"); $con->Open( "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" . "Dbq=C:\\Users\\Public\\Database1.accdb"); $rst = $con->Execute("SELECT word FROM vocab WHERE ID=3"); $stm->WriteText($rst->Fields("word")); $rst->Close(); $con->Close(); $stm->WriteText('</body>'); $stm->WriteText('</html>'); $tempFile = TEMP_FOLDER . uniqid("", TRUE) . ".txt"; $stm->SaveToFile($tempFile, 2); // adSaveCreateOverWrite $stm->Close(); echo file_get_contents($tempFile); unlink($tempFile); ?>
-<?php define("TEMP_FOLDER", "C:\\__tmp\\"); header('Content-Type: text/html; charset=utf-8'); $stm = new COM("ADODB.Stream") or die("Cannot create COM object."); $stm->Type = 2; // adTypeText $stm->Charset = 'utf-8'; $stm->Open(); $stm->WriteText('<html>'); $stm->WriteText('<head>'); $stm->WriteText('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'); $stm->WriteText('<title>ADODB test</title>'); $stm->WriteText('</head>'); $stm->WriteText('<body>'); $con = new COM("ADODB.Connection"); $con->Open( "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" . "Dbq=C:\\Users\\Public\\Database1.accdb"); $rst = $con->Execute("SELECT word FROM vocab WHERE ID=3"); $stm->WriteText($rst->Fields("word")); $rst->Close(); $con->Close(); $stm->WriteText('</body>'); $stm->WriteText('</html>'); $tempFile = TEMP_FOLDER . uniqid("", TRUE) . ".txt"; $stm->SaveToFile($tempFile, 2); // adSaveCreateOverWrite $stm->Close(); echo file_get_contents($tempFile); unlink($tempFile); ?>
-<?php define("TEMP_FOLDER", "C:\\__tmp\\"); header('Content-Type: text/html; charset=utf-8'); $stm = new COM("ADODB.Stream") or die("Cannot create COM object."); $stm->Type = 2; // adTypeText $stm->Charset = 'utf-8'; $stm->Open(); $stm->WriteText('<html>'); $stm->WriteText('<head>'); $stm->WriteText('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'); $stm->WriteText('<title>ADODB test</title>'); $stm->WriteText('</head>'); $stm->WriteText('<body>'); $con = new COM("ADODB.Connection"); $con->Open( "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" . "Dbq=C:\\Users\\Public\\Database1.accdb"); $rst = $con->Execute("SELECT word FROM vocab WHERE ID=3"); $stm->WriteText($rst->Fields("word")); $rst->Close(); $con->Close(); $stm->WriteText('</body>'); $stm->WriteText('</html>'); $tempFile = TEMP_FOLDER . uniqid("", TRUE) . ".txt"; $stm->SaveToFile($tempFile, 2); // adSaveCreateOverWrite $stm->Close(); echo file_get_contents($tempFile); unlink($tempFile); ?>
-<?php define("TEMP_FOLDER", "C:\\__tmp\\"); header('Content-Type: text/html; charset=utf-8'); $stm = new COM("ADODB.Stream") or die("Cannot create COM object."); $stm->Type = 2; // adTypeText $stm->Charset = 'utf-8'; $stm->Open(); $stm->WriteText('<html>'); $stm->WriteText('<head>'); $stm->WriteText('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'); $stm->WriteText('<title>ADODB test</title>'); $stm->WriteText('</head>'); $stm->WriteText('<body>'); $con = new COM("ADODB.Connection"); $con->Open( "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" . "Dbq=C:\\Users\\Public\\Database1.accdb"); $rst = $con->Execute("SELECT word FROM vocab WHERE ID=3"); $stm->WriteText($rst->Fields("word")); $rst->Close(); $con->Close(); $stm->WriteText('</body>'); $stm->WriteText('</html>'); $tempFile = TEMP_FOLDER . uniqid("", TRUE) . ".txt"; $stm->SaveToFile($tempFile, 2); // adSaveCreateOverWrite $stm->Close(); echo file_get_contents($tempFile); unlink($tempFile); ?>