php utf-8 特殊字符不显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8356187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:30:40  来源:igfitidea点击:

utf-8 special characters not displaying

phphtmlencodingutf-8

提问by robert

I moved my website from my local test server to NameCheap shared hosting and now I'm running into a problem - some of the pages aren't displaying utf-8 special characters properly (showing question marks instead). All pages are utf-8 encoded, as are all database tables. The strange thing is, some pages display correctly and some don't, in a seemingly random pattern.

我将我的网站从我的本地测试服务器移到 NameCheap 共享主机,现在我遇到了一个问题 - 有些页面没有正确显示 utf-8 特殊字符(而是显示问号)。所有页面都采用 utf-8 编码,所有数据库表也是如此。奇怪的是,有些页面以看似随机的模式正确显示而有些则不显示。

For instance, my index page is fine, but my profile page isn't. faq.html works fine, but when I rename it to faq.php it doesn't. And weirdest of all, I have a page with two JQuery tabs where one displays correctly and the other doesn't!

例如,我的索引页很好,但我的个人资料页不是。faq.html 工作正常,但是当我将其重命名为 faq.php 时却没有。最奇怪的是,我有一个包含两个 JQuery 选项卡的页面,其中一个显示正确,另一个显示不正确!

Can someone help me out with this?

有人可以帮我解决这个问题吗?

回答by RCE

This is really annoying problem to fix but you can try these.

这确实是一个很烦人的问题,但你可以试试这些。

First of all, make sure the file is actually saved in UTF-8 format.

首先,确保文件实际上是以 UTF-8 格式保存的。

Then check that you have <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">in your HTML header.

然后检查<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">您的 HTML 标头中是否有。

You can also try calling header('Content-Type: text/html; charset=utf-8');at the beginning of your PHP script or adding AddDefaultCharset UTF-8to your .htaccess file.

您还可以尝试header('Content-Type: text/html; charset=utf-8');在 PHP 脚本的开头调用或添加AddDefaultCharset UTF-8到 .htaccess 文件中。

回答by hakre

It sounds like that if you request faq.htmlthe webserver signals your browser that the file is in UTF-8 encoding.

如果您请求faq.html网络服务器向您的浏览器发送信号,表明该文件采用 UTF-8 编码,这听起来像是这样。

Check that with your browser which encoding is announced and used, please see the documentation of your browser how to do that. Every browser has this, most often accessible via the menu (to specify your preference which website's encoding should be used) and to see what the server returned, you often find this in page properties.

使用您的浏览器检查宣布和使用哪种编码,请参阅您的浏览器的文档如何执行此操作。每个浏览器都有这个,最常见的是通过菜单访问(指定您的偏好应该使用哪个网站的编码)并查看服务器返回的内容,您经常在页面属性中找到它。

Then it sounds like that if you request faq.phpthe webserver singals your browser that the file is in some other encoding. Probably no charset/encoding is given as per default PHP configuration setting. As it's a PHP file you can most often solve this by changing the PHP configuration default_charsetDocsdirective:

那么听起来,如果您请求faq.php网络服务器向您的浏览器发出该文件采用其他编码的信号。根据默认的 PHP 配置设置,可能没有给出字符集/编码。由于它是一个 PHP 文件,您通常可以通过更改 PHP 配置default_charset文档指令来解决此问题:

default_charset = "UTF-8"

Locate your php.ini on the host and edit it accordingly.

在主机上找到您的 php.ini 并相应地编辑它。

If you don't have the php.ini available, you can change this by code as well by using the ini_setDocsfunction:

如果您没有可用的 php.ini,您也可以使用ini_setDocs函数通过代码更改它:

ini_set('default_charset', 'UTF-8');

Take care that you change this very early in your script because PHP needs to be able to send out headers to have this working, and headers can't be set any longer if they have already been send.

请注意在脚本中尽早更改它,因为 PHP 需要能够发送标头才能使其正常工作,并且如果标头已经发送,则无法再设置它们。

Manually sending the Content-Typeheader-line does work, too:

手动发送Content-Type标题行也有效:

header('Content-Type: text/html; charset=UTF-8');

Additionally it's good practice that all the HTML pages you output have this header as well in their HTML <head>section:

此外,您输出的所有 HTML 页面都在其 HTML<head>部分中包含此标题也是一种很好的做法:

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

Hope this is helpful.

希望这是有帮助的。

回答by user2842936

set meta tag in head as

将 head 中的元标记设置为

 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 

use the link http://www.i18nqa.com/debug/utf8-debug.htmlto replace the symbols character you want.

使用链接http://www.i18nqa.com/debug/utf8-debug.html替换您想要的符号字符。

then use str_replace like

然后使用 str_replace 之类的

    $find = array('a?', 'a?', 'a|', 'a”', 'a“', 'a?', '??', '?', 'a¢', '??', 'a'); // en dash
                        $replace = array('“', ''', '…', '—', '–', '‘', 'é', '', '?', '?', '”');
$content = str_replace($find, $replace, $content);

Its the method i use and help alot. Thanks!

它的方法我使用和帮助很多。谢谢!

回答by VVV

If you're using PHP and none of the above worked (as it was my case), you need to set the locale with utf-8 encoding.

如果您使用的是 PHP 并且以上都不起作用(就像我的情况一样),则需要使用 utf-8 编码设置语言环境。

Like this

像这样

setlocale(LC_ALL, 'fr_CA.utf-8');

回答by Kamran Sohail

I solve my issue by using utf8_encode();

我通过使用 utf8_encode() 解决了我的问题;

$str = "kamé";

echo utf8_encode($str);

$str = "卡梅";

回声 utf8_encode($str);

Hope this help someone.

希望这有助于某人。

回答by Mauricio

The problem is because your file are not with the same encoding. First run the following command in all your files:

问题是因为您的文件不具有相同的编码。首先在所有文件中运行以下命令:

file -i filename.* 

In order to fix the problem you have to change all your files to uft-8. You can do it with the command iconv:

为了解决这个问题,您必须将所有文件更改为 uft-8。您可以使用命令 iconv 来完成:

iconv -f fromcode -t tocode filename > newfilename

Example:

例子:

iconv -f iso-8859-1 -t utf-8 index.html > fixed/index.html

After this you can run file -i fixedx/index.html and you will see that your file is now in uft-8

在此之后,您可以运行 file -i fixedx/index.html,您将看到您的文件现在位于 uft-8 中

回答by Binod Kalathil

If all the other answers didn't work for you, try disabling HTTP input encoding translation.

如果所有其他答案都不适合您,请尝试禁用 HTTP 输入编码转换

This is a setting related to PHP extension mbstring. This was the problem in my case. This setting was enabled by default in my server.

这是与 PHP 扩展mbstring相关的设置。这就是我的问题。默认情况下,我的服务器中启用了此设置。

回答by Hiren Parghi

Adding the following line in the head tag fixed my issue.

在 head 标签中添加以下行修复了我的问题。

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