Enn*_*ppa 4 php utf-8 diacritics character-encoding
在使用下面的代码查询mysql数据库后,我生成了一个html文件:
$myFile = "page.htm";
Run Code Online (Sandbox Code Playgroud)
$ fh = fopen($ myFile,'w')或死("无法打开文件"); fwrite($ fh,$ row ['text']); FCLOSE($ FH);
在msql db上,文本使用utf8_general_ci进行编码.但我需要将它包含在php网页中,如下所示:
<?include('page.htm');?>
Run Code Online (Sandbox Code Playgroud)
请记住,php网页在标题上使用utf8字符集:
<meta http-equiv="content-type" content="text/html; charset=utf8" />
Run Code Online (Sandbox Code Playgroud)
现在,如果我在数据库上写一些带有重音符号(èàì)或引号字符的字母,我直接打开page.htm并在数据库上看到它看起来一切正常,但是当我在php页面上查看时,我看到了问号 字符而不是我原来想要的字符.为什么?!提前致谢!
问题是html页面的编码实际上是由http响应标题'Content-Type'设置的,修复你需要做的是在任何输出之前(即在顶部)将以下内容添加到PHP文件中.
<?php
header('Content-Type: text/html; charset=utf8');
Run Code Online (Sandbox Code Playgroud)
澄清一下,那应该是包含你的html文件的PHP,而不是你包含的html文件:)
侧点: