0

I have a page with a part in php that converts a .csv table to html. However, I have a problem with accented letters. Indeed, these are not shown even if the page is set with

meta http-equiv="Content-Type" content="text/html; charset=utf-8"

and the csv file is encoded in utf-8.

The other parts not processed by the php show the accented letters with no problem. Should I add a string in the php to convert them?

Thank you so much,

Stefano

achedeuzot
  • 4,164
  • 4
  • 41
  • 56

2 Answers2

0

You can tried this, and let me know if your problem is resolved:

$string = (isUtf8($string))? $string : utf8_encode($string);

function isUtf8 ($string)
{
    //regex modified from http://www.w3.org/International/questions/qa-forms-utf-8.en.php
    return preg_match('%^(?:
          [\x09\x0A\x0D\x20-\x7E]           # ASCII
        | [\xC2-\xDF][\x80-\xBF]            # non-overlong 2-byte
        |  \xE0[\xA0-\xBF][\x80-\xBF]       # excluding overlongs
        | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
        |  \xED[\x80-\x9F][\x80-\xBF]       # excluding surrogates
        |  \xF0[\x90-\xBF][\x80-\xBF]{2}    # planes 1-3
        | [\xF1-\xF3][\x80-\xBF]{3}         # planes 4-15
        |  \xF4[\x80-\x8F][\x80-\xBF]{2}    # plane 16
    )*$%xs', $string); 
}
Ikan
  • 71
  • 3
  • 1
    What's the meaning of that? How can you be sure that the file is either utf8 or iso-8859-1 encoded? Have you copied that from somewhere and inserted here? – hek2mgl Apr 14 '14 at 16:30
  • This is the first level to resolve the same problems. you can see [w3.org](http://www.w3.org/International/questions/qa-forms-utf-8.en.php) – Ikan Apr 14 '14 at 16:43
  • If so, you might have added the origin of this information to your answer. However, the main problem I wanted to point out is the fact that you are using `utf8_encode()` here. `utf8_encode()` is capable if transcoding from iso-8859-1 to utf8 and not more. Meaning you can't use `utf8_encode()` to transcode strings with arbitrary encodings to utf8 – hek2mgl Apr 14 '14 at 16:52
0

You can put all your return in the following PHP function:

string utf8_encode ( string $data )

You can include all your text without problem.

Font: http://www.php.net/manual/en/function.utf8-encode.php

Lucas Henrique
  • 1,380
  • 1
  • 11
  • 15