简体   繁体   English

PHP UTF-8字符集强调

[英]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 稍后在php中,我使用以下行

$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>" ; $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) Itajubáem Foco Canal20 Oficina de Cuidados Paliativos(正确)

Visualiza es: 204 //(incorrect) 可视化:204 //(不正确)

if i try 如果我尝试

echo utf8_encode($v);

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

Visualizações: 204 //(correct) 可视化:204 //(正确)

I tried to use the 我尝试使用

header('Content-type: text/html; charset=utf-8'); 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! 确保您使用的编辑器将文件保存在UTF-8中! Some of this stuff seems to be coming from a database where it has to be stored in UTF-8 (alternatively do 其中一些东西似乎来自必须存储在UTF-8中的数据库(或者

mysql_query("SET NAMES utf8");

to switch the connection to UTF-8. 将连接切换到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". 如果您的编辑器未将文本(“Visualizaçes”)保存为UTF-8,则应使用编辑器或“ 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. 如果来自数据库的数据以UTF-8格式输出正常,但是.php文件本身中的静态内容不是,则意味着您没有在文本编辑器中将PHP文件本身保存为UTF-8。 。 Probably you have saved as Windows code page 1252 (Western European); 可能您已将其另存为Windows代码页1252(西欧); 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). 确保在保存对话框上将其更改(更改为UTF-8;如果“没有BOM的UTF-8”有单独的选项,请选择该选项,因为带有BOM的UTF-8完全是虚假的)。

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. 如果您的文本编辑器稍后在将文件重新加载时出现错误的符号,则您需要告诉它默认情况下以UTF-8模式加载文件。 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: 如果您真的无法替换它,则必须放弃源文件中的Unicode字符并将其编码为HTML:

Visualiza&#xE7;&#xF5;es

or, specifically in a PHP string literal: 或者,特别是在PHP字符串文字中:

"Visualiza\xE7\xF5es"

incidentally you need to be using htmlspecialchars when you're putting text into HTML, as otherwise you've got XSS bugs. 顺便说一句,当您将文本放入HTML时,您需要使用htmlspecialchars ,否则您将遇到XSS错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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