tags:

views:

83

answers:

2

Hi guys,

Sorry if this question is already somewhere but I need a more pratical approach:

The page is set like this:

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

later in php i use the following line

$v .= "<a href='" . $videoEntry->getVideoWatchPageUrl() . "' rel='vidbox' title='" . $videoEntry->getVideoTitle() . "'><img src='";

...

$v .= "<td width='52%' height='16'><strong>Visualizações:</strong> " . $videoEntry->getVideoViewCount() . "</td>";

ok. now

When i do

echo $v;

Itajubá em Foco Canal20 Oficina de Cuidados Paliativos (correct)

Visualiza��es: 204 //(incorrect)

if i try

echo utf8_encode($v);

Itajubá em Foco Canal20 Oficina de Cuidados Paliativos //(incorrect)

Visualizações: 204 //(correct)

I tried to use the

header('Content-type: text/html; charset=utf-8');

but with no success.

+1  A: 

Make sure that the editor you are using is saving the files in UTF-8! Some of this stuff seems to be coming from a database where it has to be stored in UTF-8 (alternatively do

mysql_query("SET NAMES utf8");

to switch the connection to UTF-8.

If the text ("Visualizações") is not saved as UTF-8 by your editor you should convert the file using either your editor or another tool like "iconv".

Best wishes,
Fabian

halfdan
If your php version supports it, better use mysql_set_charset() instead of SET NAMES. This way the client library "knows" about the change of encoding and mysql_real_escape_string() is ensured to work properly, http://docs.php.net/mysql_set_charset
VolkerK
And read more about the connection encoding at http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
VolkerK
+1  A: 

If the data from the database is coming out OK as UTF-8, but the static content you have in the .php file itself isn't, that means you're not saving your PHP file itself as UTF-8 in your text editor. Probably you have saved as Windows code page 1252 (Western European); make sure to change that on the save dialogue box (to UTF-8; if there is a separate option for ‘UTF-8 without BOM’ choose that, as UTF-8-with-BOM is utterly bogus).

If your text editor later gets the symbols wrong when it loads the file back, you'll need to tell it to load file by default in UTF-8 mode. If it doesn't have that option, it's rubbish and should be replaced. If you really can't replace it you will have to forgo Unicode characters in your source file and encoding them, either as HTML:

Visualiza&#xE7;&#xF5;es

or, specifically in a PHP string literal:

"Visualiza\xE7\xF5es"

incidentally you need to be using htmlspecialchars when you're putting text into HTML, as otherwise you've got XSS bugs.

bobince
Yes the problem is on my editor. But....When i set to UTF-8 (withcou BOM) the "Visualizações" become "Visualizaes".
Paulo Bueno
‘becomes’? When you load it back into the editor? When you view the PHP output in a web browser?
bobince