繁体   English   中英

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

[英]How can I ask DOMDocument to not parse html 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... ...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特殊字符?
谢谢

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

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

尝试:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM