0

While moving my websites to a new server, a found the following issue: the rendering of 'strange' characters (ê,ç,ã,etc) coming from mysql are being rendered wrong.

For example:

MySQL: Galo vai a final, após cobranças de pênaltis.

Old server (apache 2.2 /php 5.3): Galo vai a final, após cobranças de pênaltis. (Correct)

New server (apache 2.4 /php 5.4): Galo vai a final, após cobranças de pênaltis. (Wrong)

I belive that either apache or php is causing this but I haven't found documentantion about it anywhere.

Can someone help me to find the cause for this error?

EDIT

Here's the code to render this (note that it's the same in both servers):

Controller:

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

View:

<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>   
Robyflc
  • 1,209
  • 11
  • 16
  • incorrect encoding. check the encoding settings in apache – DevZer0 Jul 12 '13 at 12:50
  • 2
    http://www.php.net/manual/en/function.htmlspecialchars.php#112476 `As of PHP 5.4 they changed default encoding from "ISO-8859-1" to "UTF-8"` worth looking into as a possible cause? – Flosculus Jul 12 '13 at 12:51
  • Do you have a content type specified in the html? Something like: `` – user254875486 Jul 12 '13 at 12:52
  • @Flosculus I believe he is just echoing the string. – silentw Jul 12 '13 at 12:52
  • Maybe this answer is helpful: http://stackoverflow.com/questions/913869/how-to-change-the-default-encoding-to-utf-8-for-server – joe776 Jul 12 '13 at 12:52
  • @Flosculus This change only applies to the encoding of special characters that cannot directly be displayed in HTML like `&` or `>`. – joe776 Jul 12 '13 at 12:55
  • @joe776 fair enough, i was just looking into any changes to character encoding in 5.4 – Flosculus Jul 12 '13 at 12:56
  • See [UTF-8 all the way through](http://stackoverflow.com/a/279279). – eggyal Jul 12 '13 at 13:05
  • @Lex, I have in the code. – Robyflc Jul 12 '13 at 13:41
  • @silentw, if I change utf-8 to 8859, the rest of the page goes wrong and the parts that are already wrong get worse, like: Galo vai a final, após cobranças de pênaltis. – Robyflc Jul 12 '13 at 13:43
  • You are seing UTF-8 characters interpreted in a single-byte encoding (perhaps ISO-8859-1).Either your database was reimported wrong (original dump was UTF-8 and it was imported in a ISO-something encoding), or you are reading correct UTF-8 data and interpret it as something else. Please show us your test code. – RandomSeed Jul 12 '13 at 13:43
  • @RandomSeed, I added code to the question but I don't think it'll help since it's the same on both servers. Also, the database is the same in both too. – Robyflc Jul 12 '13 at 13:59
  • It sure helps, because there we can see that you use an external library to access your database. Those who know about it will likely be able to advise the relevant settings to verify (I am afraid I have no clue). – RandomSeed Jul 12 '13 at 14:20

1 Answers1

0

You have to change the encoding manually to "ISO-8859-1". Before 5.4, the default encoding was "UTF-8". Use this code:

htmlspecialchars($string, ENT_COMPAT,'ISO-8859-1', true)
albin
  • 194
  • 3
  • 13
  • As I said, he probably is only just echoing the string, not using `htlmspecialchars`. In my experience, the correct charset in the meta tags is enough. – silentw Jul 12 '13 at 12:58
  • 2
    `htmlspecialchars` does not apply here. It only applies to e.g. `<` or `>` not special characters of the language. And it's the other way round: Now the default encoding is UTF-8, before 5.4 it was ISO-8859-1 – joe776 Jul 12 '13 at 12:59