简体   繁体   中英

PHP UTF-8 charset accentuation

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>" $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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM