如何让DOMDocument不解析html特殊字符?

时间:2014-05-21 22:36:18

标签: php dom special-characters

(对不起我的英文)我有这段代码

$content2 = file_get_contents( $url );
$dom2 = new DOMDocument();
@$dom2->loadHTML( $content2 );
$classname = 'b-details_text';
$finder = new DomXPath($dom2);
$parent = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
$ps = $parent->item( 0 )->getElementsByTagName( "p" );

$text = $ps->item( 0 )->textContent;

其中$ content2包含此部分

...
<div>
<div class='b-details_text'>

<p> ...It&#8217;s the best way to get good love karma flowing &#8212; and you&#8217;ll be surprised by how good it makes you feel right away, too... </p>
</div>
</div>
...

我希望得到     ...It&#8217;s the best way to get good love karma flowing &#8212; and you&#8217;ll be surprised by how good it makes you feel right away, too...  在$ text

但相反,我总是得到类似...ItтАЩs the best way to get good love karma flowing тАФ and youтАЩll be surprised by how good it makes you feel right away, too...

的内容

如何让DOMDocument不解析html特殊字符?
感谢

1 个答案:

答案 0 :(得分:0)

在节点文本上使用htmlentities()可能会解决您的问题。

此解决方案不会阻止DOMDocument首先解析html实体。

尝试:

$text = htmlentities($ps->item( 0 )->textContent);