Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-html_实体_解码并非解码所有内容_Php_Html_Parsing_Dom - Fatal编程技术网

PHP-html_实体_解码并非解码所有内容

PHP-html_实体_解码并非解码所有内容,php,html,parsing,dom,Php,Html,Parsing,Dom,我正在解析一个HTML页面。在某个时刻,我得到了div和using之间的文本 html_实体_解码以打印该文本 问题是页面包含类似于此星号的字符★或其他类似形状的⬛︎, ◄, ◉, 等等,我已经检查过了,这些字符没有在源代码页上编码,它们就像你正常看到的一样 页面正在使用charset=“UTF-8” 所以,当我使用 html_entity_decode($string, ENT_QUOTES, 'UTF-8'); 例如,星号被“解码”为 $string是通过使用获取的 document.ge

我正在解析一个HTML页面。在某个时刻,我得到了div和using之间的文本 html_实体_解码以打印该文本


问题是页面包含类似于此星号的字符
或其他类似形状的⬛︎, ◄, ◉, 等等,我已经检查过了,这些字符没有在源代码页上编码,它们就像你正常看到的一样

页面正在使用charset=“UTF-8”

所以,当我使用

html_entity_decode($string, ENT_QUOTES, 'UTF-8');
例如,星号被“解码”为

$string是通过使用获取的

document.getElementById("id-of-div").innerText
我想正确地解码它们。如何在PHP中实现这一点


注意:我试过
htmlspecialchars\u解码($string,ENT\u引号),它会产生相同的结果。

我试图用这个简单的PHP代码重现您的问题:

<?php
  // Make sure our client knows we're sending UTF-8
  header('Content-Type: text/plain; charset=utf-8');
  $string = "The page contains characters like this star ★ or others like shapes like ⬛︎, ◄, ◉, etc. Here are some entities: This is a &quot;test&quot;.";
  echo 'String: ' . $string . "\n";
  echo 'Decoded: ' . html_entity_decode($string, ENT_QUOTES, 'UTF-8');
如果我将标题中的字符集更改为
iso-8859-1
,我会看到:

String: The page contains characters like this star ★ or others like shapes like ⬛︎, ◄, ◉, etc. Here are some entities: <span>This is a &quot;test&quot;.
Decoded: The page contains characters like this star ★ or others like shapes like ⬛︎, ◄, ◉, etc. Here are some entities: <span>This is a "test".
String:页面包含类似于这个星型的字符,或者其他类似于形状的字符,如–›êŽŽ、-°等。下面是一些实体:这是一个“测试”。
解码:该页面包含类似于此星号的字符,或其他类似于形状的字符,如–›êŽŽ、–等。以下是一些实体:这是一个“测试”。

所以,我认为你的问题是一个显示问题。正如您所期望的那样,html\u entity\u decode
完全没有触及“有趣”字符。只是不管你有什么代码,或者你用什么来查看你的输出,都不正确地使用iso-8859-1来显示它们。

我试图用这个简单的PHP来重现你的问题:

<?php
  // Make sure our client knows we're sending UTF-8
  header('Content-Type: text/plain; charset=utf-8');
  $string = "The page contains characters like this star ★ or others like shapes like ⬛︎, ◄, ◉, etc. Here are some entities: This is a &quot;test&quot;.";
  echo 'String: ' . $string . "\n";
  echo 'Decoded: ' . html_entity_decode($string, ENT_QUOTES, 'UTF-8');
如果我将标题中的字符集更改为
iso-8859-1
,我会看到:

String: The page contains characters like this star ★ or others like shapes like ⬛︎, ◄, ◉, etc. Here are some entities: <span>This is a &quot;test&quot;.
Decoded: The page contains characters like this star ★ or others like shapes like ⬛︎, ◄, ◉, etc. Here are some entities: <span>This is a "test".
String:页面包含类似于这个星型的字符,或者其他类似于形状的字符,如–›êŽŽ、-°等。下面是一些实体:这是一个“测试”。
解码:该页面包含类似于此星号的字符,或其他类似于形状的字符,如–›êŽŽ、–等。以下是一些实体:这是一个“测试”。

所以,我认为你的问题是一个显示问题。正如您所期望的那样,html\u entity\u decode
完全没有触及“有趣”字符。只是不管你有什么代码,或者你用什么来查看你的输出,都不正确地使用iso-8859-1来显示它们。

1。star是否具有等效的HTML实体?2.那么,
$string
包含什么呢?3.对我来说,这似乎是一个字符编码问题。我一点也不知道。2.理论上,包含在特定第3部分中的所有字符串。我不确定。“我已经检查过了,这些字符没有在源页面上编码……我想正确地解码。”如果它们没有编码,你希望如何准确地解码它们
html\u entity\u decode
纯粹是关于转换表单的实体
&什么(包括
某物的数值
)转换为“实”字符。这里的内容看起来像一个UTF-8字符串,然后在非UTF-8上下文中进行回音。部分问题是,你为什么要这么做?如果你有一些UTF-8字符要打印出来,你为什么还要进行html\u实体\u解码呢?为什么不把它们打印出来呢?我们可以看看源文档和实际代码的例子吗?我刚刚测试了你问题中字符的html_entity_decode,正如预期的那样,它保持了它们不变。您是如何创建输出的,您是如何看待它的?我的猜测是:html_entity_decode是一种转移注意力的方法,您实际上输出的是未经处理的UTF-8字符,但您的字符编码错误,因此它们在显示时会被损坏。1。star是否具有等效的HTML实体?2.那么,
$string
包含什么呢?3.对我来说,这似乎是一个字符编码问题。我一点也不知道。2.理论上,包含在特定第3部分中的所有字符串。我不确定。“我已经检查过了,这些字符没有在源页面上编码……我想正确地解码。”如果它们没有编码,你希望如何准确地解码它们
html\u entity\u decode
纯粹是关于转换表单的实体
&什么(包括
某物的数值
)转换为“实”字符。这里的内容看起来像一个UTF-8字符串,然后在非UTF-8上下文中进行回音。部分问题是,你为什么要这么做?如果你有一些UTF-8字符要打印出来,你为什么还要进行html\u实体\u解码呢?为什么不把它们打印出来呢?我们可以看看源文档和实际代码的例子吗?我刚刚测试了你问题中字符的html_entity_decode,正如预期的那样,它保持了它们不变。您是如何创建输出的,您是如何看待它的?我的猜测是:html_实体_解码是一种转移注意力的方法,实际上你输出的是未经处理的UTF-8字符,但是你的字符编码是错误的,所以它们在显示时会被弄乱。你是对的!这是个好主意!我忘了添加
标题('Content-Type:text/html;charset=utf-8')到代码的开头,因此它将强制输出UTF-8。谢谢你是对的!这是个好主意!我忘了添加
标题('Content-Type:text/html;charset=utf-8')到代码的开头,因此它将强制输出UTF-8。谢谢