2013-07-12 115 views
0

将我的网站移动到新的服务器时,发现了以下问题:来自mysql的“奇怪”字符(ê,ç,ã等)的呈现出现错误。为什么使用相同的数据会得到不同的结果?

例如:

MySQL的:加洛VAI最终,apóscobranças德pênaltis。

旧服务器(apache 2.2/php 5.3): Galo vai a final,apóscobrançasdepênaltis。 (正确)

新的服务器(Apache的2.4/PHP的5.4): 加洛VAI最终,apóscobranças德pênaltis。 (错)

我相信,无论是Apache或PHP是造成这一点,但我还没有找到关于它在任何地方的文件。

有人能帮我找到这个错误的原因吗?

编辑

这里呈现此代码(注意,这是相同的两个服务器):

控制器:

//Noticias 
$q = Doctrine_Query::create() 
    ->select("id,titulo,thumb,conteudo") 
    ->from('noticia') 
    ->limit(10) 
    ->where("data <= '$today'") 
    ->andWhere("status = 1") 
    ->andWhere("categoria_id = 1") 
    ->orderby('data DESC'); 
$res = $q->fetchArray(); 
foreach($res as $key => $value){ 
    $res[$key]['titulo'] = stripslashes($res[$key]['titulo']); 
    $res[$key]['conteudo'] = strip_tags($res[$key]['conteudo']); 
    $res[$key]['conteudo'] = stripslashes($res[$key]['conteudo']); 
    $res[$key]['conteudo'] = substr($res[$key]['conteudo'],0,150) . '...'; 
} 
$smarty -> assign('noticias',$res); 

查看:

<ul class="homelist"> 
    {foreach item=noticia from=$noticias name=news} 
    <li> <a href="noticia/{$noticia.id}/{$noticia.titulo|slug}/"><img src="assets/images/noticias/{$noticia.thumb}" alt="" /></a> <a class="title" href="noticia/{$noticia.id}/{$noticia.titulo|slug}/">{$noticia.titulo}</a> 
     <p>{$noticia.conteudo}</p> 
     <a class="more" href="noticia/{$noticia.id}/{$noticia.titulo|slug}/">more</a> 
    </li> 
    {/foreach} 
    <li><p align="center"><a href="/noticias/1">Veja mais</a></p> </li> 
    </ul> 
+0

不正确的编码。检查apache的编码设置 – DevZer0

+0

你的字符集是什么? – silentw

+2

http://www.php.net/manual/en/function.htmlspecialchars.php#112476 '从PHP 5.4开始,它们将默认编码从“ISO-8859-1”更改为“值得查看的UTF-8”作为可能的原因? – Flosculus

回答

0

您必须手动将编码更改为“ISO-8859-1”。在5.4之前,默认编码是“UTF-8”。 使用此代码:

htmlspecialchars($string, ENT_COMPAT,'ISO-8859-1', true) 
+0

正如我所说,他可能只是回应字符串,不使用'htlmspecialchars'。根据我的经验,meta标签中正确的charset就足够了。 – silentw

+2

'htmlspecialchars'不适用于此。它只适用于例如'<' or '>'不是该语言的特殊字符。反过来说:现在默认编码是UTF-8,5.4之前是ISO-8859-1 – joe776

相关问题