php html转dom,php – 防止DOMDocument :: loadHTML()转换实体

我有一个字符串值,我正在尝试提取列表项.我想提取文本和任何子节点,但是,DOMDocument正在将实体转换为角色,而不是保留原始状态.

我已经尝试将DOMDocument :: resolveExternals和DOMDocument :: substituteEntities设置为false,但这没有任何效果.应该注意我使用PHP 5.2.17在Win7上运行.

示例代码是:

$example = '

  • text'.

'

½ of this is strong';

echo 'To be converted:'.PHP_EOL.$example.PHP_EOL;

$doc = new DOMDocument();

$doc->resolveExternals = false;

$doc->substituteEntities = false;

$doc->loadHTML($example);

$domNodeList = $doc->getElementsByTagName('li');

$count = $domNodeList->length;

for ($idx = 0; $idx < $count; $idx++) {

$value = trim(_get_inner_html($domNodeList->item($idx)));

/* remainder of processing and storing in database */

echo 'Saved '.$value.PHP_EOL;

}

function _get_inner_html( $node ) {

$innerHTML= '';

$children = $node->childNodes;

foreach ($children as $child) {

$innerHTML .= $child->ownerDocument->saveXML( $child );

}

return $innerHTML;

}

&安培; frac12;最终转换为½(单字符/ UTF-8版本,而不是实体版本),这不是所需的格式.

解决方法:

不是PHP 5.3.6的解决方案

$html =<<

  • text
  • ½ of this is strong

HTML;

$doc = new DOMDocument();

$doc->resolveExternals = false;

$doc->substituteEntities = false;

$doc->loadHTML($html);

foreach ($doc->getElementsByTagName('li') as $node)

{

echo htmlentities(iconv('UTF-8', 'ISO-8859-1', $node->nodeValue)), "\n";

}

标签:php,dom,domdocument,php-5-2

来源: https://codeday.me/bug/20190613/1235957.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值