使用 php 修复 html 文档上显示不正确的编码

2021-12-27 00:00:00 encoding php phpquery

有没有办法修复通过phpquery::newDocument运行这个html标记后显示不正确的字符?在使用 phpquery 创建新文档后,原始文档中的 -Classics with modern Woman- 周围有预定的双引号.

Is there a way to fix the characters that display improperly after running this html markup through phpquery::newDocument? There are slated double quotes around -Classics with modern Woman- in the original document that end up displaying improperly after creating the new doc with phpquery.

    //Original document is UTF-8 encoded
$raw_html = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body><p>Mr. Smith of Bangkok celebrated the "Classics with modern Woman".</p></body></html>';
print($raw_html);

$aNew_document = phpQuery::newDocument($raw_html);
print($aNew_document);

原始输出:曼谷史密斯先生庆祝经典与现代女性".

Original Output: Mr. Smith of Bangkok celebrated the "Classics with modern Woman".

新文档输出:曼谷的史密斯先生与现代女性一起庆祝经典".

New Document Output: Mr. Smith of Bangkok celebrated the �Classics with modern Woman.

推荐答案

  1. 您需要使用UTF-8 without BOM 编码保存页面.
  2. 在脚本顶部添加此标题:

  1. You need to save the page with UTF-8 without BOM encoding.
  2. Add this header on top of your script:

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

:如何在没有 BOM 的情况下将文件另存为 UTF-8 :

根据 OP 要求,您可以在 Windows 上执行以下操作:

On OP request, here's how you can do on Windows:

  1. 下载Notepad++.这是一个很棒的文本编辑器,您应该使用它.
  2. 安装它.
  3. 在 Notepad++ 中打开包含此代码的 PHP 脚本.您正在执行所有编码的页面.是的,您计算机上的那个文件.
  4. 在 Notepad++ 中,从顶部的编码菜单中,选择不带 BOM 转换为 UTF-8".
  5. 保存文件.
  6. 通过 FTP 或您使用的任何方式上传到您的网络服务器.
  7. 现在,运行该脚本.
  1. Download Notepad++. It is an awesome text-editor that you should be using.
  2. Install it.
  3. open the PHP script in Notepad++ that contains this code. The page where you are doing all the coding. Yes, that file on your computer.
  4. In Notepad++, from the Encoding menu at the top, select "Convert to UTF-8 without BOM".
  5. Save the file.
  6. Upload to your webserver by FTP or whatever you use.
  7. Now, run that script.

相关文章