无法从Access获取正确的utf-8文本 [英] Cannot get the correct utf-8 text from Access

查看:87
本文介绍了无法从Access获取正确的utf-8文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图从数据库中获取中文字符时,我得到了奇怪的文本. 我几乎尝试了所有操作,例如html_entity_decodehtmlentities,使用utf-8保存文件,使用utf-8编码,但是我似乎无法正确处理.

When I tried to get chinese characters from the database, I got weird text. I tried almost everything, like html_entity_decode, htmlentities, save the file using utf-8, encode in utf-8, but I can't seem to get it right.

我如何获得正确的文字?

How do i get the right text?

这是我的代码:

<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="&#37806;";
//$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.

ps: I am using Traditional Chinese version of MS Office.

推荐答案

前一段时间,我遇到了这个问题,使它正常工作的唯一方法是将HTML写入ADODB.Stream对象,然后将其保存到文件,然后echo该文件:

I encountered this issue a while ago and the only way I could get it to work was to write the HTML into an ADODB.Stream object, save it to a file, and then echo the file:

<?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);
?>

这篇关于无法从Access获取正确的utf-8文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆