PHP DOM:获取除子节点之外的NodeValue

时间:2017-04-28 15:52:22

标签: php html domdocument

我有一个变量$content,其中包含一段HTML代码:



<b>AMZN 466.00 ( 15743 ) ( <span class='red'> -1 </span>) 
MSFT 290.00 ( 37296 ) ( <span class='red'> -2 </span>)
TWTR 4,000.00 ( 20 ) ( <span class=''> 0 </span>)</b>
&#13;
&#13;
&#13;

现在,我希望<b>的值使用PHP DOM排除<span>的值。如何才能做到这一点?代码片段会很有帮助。

到目前为止,我已经尝试过这个:

$dom = new domDocument;
@$dom->loadHTML($content);
$contents_i_want = $dom->getElementsByTagName('b');
foreach($contents_i_want as $content_i_want){ 
   $filtered_content = $content_i_want->nodeValue;
   echo $filtered_content;
}

1 个答案:

答案 0 :(得分:3)

希望这会帮助你..

Try this code snippet here

<?php
ini_set('display_errors', 1);
$string='<html><body><b>AMZN 466.00 ( 15743 ) ( <span class=\'red\'> -1 </span>) 
MSFT 290.00 ( 37296 ) ( <span class=\'red\'> -2 </span>)
TWTR 4,000.00 ( 20 ) ( <span class=\'\'> 0 </span>)</b></body></html>';

$dom = new DOMDocument();
$dom->loadHTML($string);
$dom->getElementsByTagName("b");

$xpath= new DOMXPath($dom);
$result=$xpath->query("//b/span");//here we are querying domdocument to find span which is inside b.

$nodesToRemove=array();//here we are maintaining an array of nodes which we want to remove
foreach($result as $node)
{
    $node->parentNode->removeChild($node);//removing nodes from its parent
}
 echo $dom->getElementsByTagName("b")->item(0)->textContent;//displaying content after removing nodes.

<强>输出:

AMZN 466.00 ( 15743 ) ( ) 
MSFT 290.00 ( 37296 ) ( )
TWTR 4,000.00 ( 20 ) ( )
相关问题