php PHP包含html页面字符集问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3162197/
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
PHP include html page charset problem
提问by EnneKappa
after querying a mysql db using the code below i have generated an html file:
使用下面的代码查询 mysql 数据库后,我生成了一个 html 文件:
$myFile = "page.htm";
$fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, $row['text']); fclose($fh);
$fh = fopen($myFile, 'w') or die("无法打开文件"); fwrite($fh, $row['text']); fclose($fh);
On the msql db the text is encoded using utf8_general_ci. But i need to include it in a php web page as shown below:
在 msql 数据库中,文本使用 utf8_general_ci 编码。但我需要将它包含在一个 php 网页中,如下所示:
<?include('page.htm');?>
bearing in mind that the php web page uses utf8 charset on the header:
请记住,php 网页在标题上使用 utf8 字符集:
<meta http-equiv="content-type" content="text/html; charset=utf8" />
Now if i write on the db some letters with grave accent (è à ì) or a quote character and i open directly page.htm and on the db i see it all looking ok, but when i view it on the php page i see a question mark ? character instead of those I originally wanted. Why?! Thanks in advance!
现在,如果我在 db 上写一些带有重音 (èàì) 或引号字符的字母,然后我直接打开 page.htm,在 db 上我看到它看起来一切正常,但是当我在 php 页面上查看它时,我看到问号?字符而不是我最初想要的那些。为什么?!提前致谢!
回答by nathan
The problem is that the encoding of the html page is actually set by the http response header 'Content-Type', to fix what you need to do is add the following to your PHP file before any output (ie at the top).
问题是 html 页面的编码实际上是由 http 响应头“Content-Type”设置的,要修复您需要做的是在任何输出之前(即在顶部)将以下内容添加到您的 PHP 文件中。
<?php
header('Content-Type: text/html; charset=utf8');
To clarify, that should be in the PHP that includes your html file, notin the html file you include :)
澄清一下,这应该在包含您的 html 文件的 PHP 中,而不是在您包含的 html 文件中:)
side point(s):
侧点:
- It's good practise to use the full opening tag <?php rather than <? as this isn't supported by all (many) servers
- include is a statement not a function, so typically you'd write: include 'page.htm';
- 使用完整的开始标签 <?php 而不是 <? 因为并非所有(许多)服务器都支持此功能
- include 是一个语句而不是一个函数,所以通常你会写: include 'page.htm';
回答by fabrik
META charset isn't always solve the problem. Make sure your IDE saving real UTF-8 files. For exanple in Dreamweaver press CTRL-J then check Title/Encoding options.
META 字符集并不总能解决问题。确保您的 IDE 保存了真正的 UTF-8 文件。例如,在 Dreamweaver 中按 CTRL-J,然后检查标题/编码选项。
回答by EnneKappa
I solved adding this header('Content-Type: text/html; charset=iso-8859-1');in the php webpage with the include.
I don't know why it works. I never used iso-8859-1 charset.
Thanks anyway!
我解决header('Content-Type: text/html; charset=iso-8859-1');了在包含包含的 php 网页中添加它的问题。我不知道它为什么有效。我从未使用过 iso-8859-1 字符集。不管怎么说,还是要谢谢你!
回答by ChrisW
If you are getting stuff from a database, and it changes charset inexplicitly, note that in Php the actual connection charset to the db needs to be set explicitly to utf8 (if that's what you use), otherwise the content is converted while transfered, even if the content in the db itself is in correct format.. an interesting quirk ;)
如果您从数据库获取内容,并且它不明确地更改了字符集,请注意,在 Php 中,需要将与数据库的实际连接字符集显式设置为 utf8(如果您使用的是 utf8),否则内容会在传输时进行转换,即使如果数据库本身的内容格式正确……一个有趣的怪癖;)
Like so: mysql_select_db($database, $connect); mysql_set_charset('utf8', $connect); // set the connection charset.
像这样: mysql_select_db($database, $connect); mysql_set_charset('utf8', $connect); // 设置连接字符集。
回答by Unicron
The problem is probably with a wrong encoding set in your mySQL database, or the database connection.
问题可能是在您的 mySQL 数据库或数据库连接中设置了错误的编码。
If your tables are all 100% utf8_general_ci, try doing a mysql_query("SET NAMES utf8;");before doing any queries: That will set the connection to UTF-8.
如果您的表都是 100% utf8_general_ci,请mysql_query("SET NAMES utf8;");在执行任何查询之前尝试执行以下操作:这会将连接设置为 UTF-8。
回答by Hamed MP
One point about your code:
关于您的代码的一点:
when you use include, it is a php code, so you should include a php file, not a htm file:
当你使用include时,它是一个php代码,所以你应该包含一个php文件,而不是一个htm文件:
<?include('page.php');?>
try changing the extension, I don't know how your code were working till now :)
尝试更改扩展名,直到现在我不知道你的代码是如何工作的:)
回答by nimura
Try separate includes blocks. Ex mainpage.php:
尝试单独的包含块。前 mainpage.php:
<?php
include("php1.php");
?>
<?php
include("php2.php");
?>enter code here
回答by will
I know i'm 10 years late for OP, but I encountered same problem. I was doing an include of a php file (I supose it's the same for html file) that wouldn't show utf-8. I couldn't use meta as this include comes after displaying html.
我知道我 OP 晚了 10 年,但我遇到了同样的问题。我正在做一个不会显示 utf-8 的 php 文件的包含(我假设它与 html 文件相同)。我不能使用元,因为这个包含在显示 html 之后出现。
My way to solve:
我的解决方法:
- Copied whole text of php into a new file.
- Replaced original php with new file.
- 将 php 的整个文本复制到一个新文件中。
- 用新文件替换原来的 php。
I hope it helps, i supose some meta data in original file was breakig everyting. But this is jsut a guess.
我希望它有所帮助,我假设原始文件中的一些元数据是破坏性的。但这只是一个猜测。

