PHP HTML编码 [英] PHP HTML encoding

查看:167
本文介绍了PHP HTML编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析一个HTML页面,但编码是乱糟糟的我的结果。经过一些研究,我发现了一个非常流行的解决方案使用 utf8_encode() utf8_decode()任何东西。在下面几行中,您可以检查我的代码和输出。

I'm trying to parse a HTML page, but the encoding is messing my results. After some research I found a very popular solution using utf8_encode() and utf8_decode(), but it doesn't change anything. In the following lines, you can check my code and the output.

$str_html = $this->curlHelper->file_get_contents_curl($page);
$str_html = utf8_encode($str_html);

$dom = new DOMDocument();
$dom->resolveExternals = true;
$dom->substituteEntities = false;
@$dom->loadHTML($str_html);
$xpath = new DomXpath($dom);

(...)
$profile = array();
for ($index = 0; $index < $table_lines->length; $index++) {
    $desc = utf8_decode($table_lines->item($index)->firstChild->nodeValue);
}



输出



Output

Testar é bom

应该

Testar é bom



我尝试过的内容




  • htmlentities():

    What I've tried

    • htmlentities():

      htmlentities($ table_lines-> item($ index) - > lastChild-> nodeValue,ENT_NOQUOTES,ini_get('ISO-8859-1'),false);

      htmlspecialchars():

      htmlspecialchars():

      htmlspecialchars ($ table_lines-> item($ index) - > lastChild-> nodeValue,ENT_NOQUOTES,'ISO-8859-1',false);

      按照此处更改我的文件的字符集。

      Change my file's charset as decribed here.


      • 网站编码:< meta http-equiv =content-typecontent =text / html; charset = ISO-8859-1/>

      • Website encoding: <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />

      提前感谢!

      推荐答案

      $ c> utf8_decode():

      Try using the following without a prior utf8_decode():

      mb_convert_encoding($str, 'ISO-8859-1', 'UTF-8');
      

      或者,不要使用 utf8_decode()并尝试将您的网站meta更改为:

      Alternatively, don't use utf8_decode() and try to change your website meta to:

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







      • mb_convert_encoding()

      • 这篇关于PHP HTML编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆