>백엔드 개발 >PHP 튜토리얼 >使用PHP XPath采集的时候,如何保留nodeValue里的html符号

使用PHP XPath采集的时候,如何保留nodeValue里的html符号

WBOY
WBOY원래의
2016-06-06 20:29:451749검색

代码如下:

<code>$html = 


    <meta charset="UTF-8">
    <title>Test</title>


<div id="content">
  <p>
    <span>
      abcdefghijklmn<br>opqrstuvwxyz
    </span>
  </p>
</div>



EOF;
// create document object model
$dom = new DOMDocument();
// load html into document object model
@$dom->loadHTML($html);
// create domxpath instance
$xPath = new DOMXPath($dom);
// get all elements with a particular id and then loop through and print the href attribute
$elements = $xPath->query('//*[@id="content"]/p/span');
$content = $elements->item(0)->nodeValue;
echo $content;</code>

内容里的<br>会被去除,使用什么操作比如有没有$e->innerHtml之类的,可以保留html标签。

8.18 更新:

<code>$html = 


    <meta charset="UTF-8">
    <title>Test</title>


<div id="content">
  <p>
    <span class="aaa">
      abcdefghijklmn<br><span>opq</span>rstuvwxyz
    </span>
  </p>
</div>



EOF;

// create document object model
$dom = new DOMDocument();
// load html into document object model
@$dom->loadHTML($html);
// create domxpath instance
$xPath = new DOMXPath($dom);
// get all elements with a particular id and then loop through and print the href attribute
$elements = $xPath->query('//*[@id="content"]/p/span');
$nodeName = $elements->item(0)->nodeName;
// $content = $elements->item(0)->nodeValue;
$content = $dom->saveXml($elements->item(0));
$content = $dom->saveHtml($elements->item(0));
$content = preg_replace(array("#^#isU", "#{$nodeName}>$#isU"), array('', ''), $content);
echo $content;</code>

回复内容:

代码如下:

<code>$html = 


    <meta charset="UTF-8">
    <title>Test</title>


<div id="content">
  <p>
    <span>
      abcdefghijklmn<br>opqrstuvwxyz
    </span>
  </p>
</div>



EOF;
// create document object model
$dom = new DOMDocument();
// load html into document object model
@$dom->loadHTML($html);
// create domxpath instance
$xPath = new DOMXPath($dom);
// get all elements with a particular id and then loop through and print the href attribute
$elements = $xPath->query('//*[@id="content"]/p/span');
$content = $elements->item(0)->nodeValue;
echo $content;</code>

内容里的<br>会被去除,使用什么操作比如有没有$e->innerHtml之类的,可以保留html标签。

8.18 更新:

<code>$html = 


    <meta charset="UTF-8">
    <title>Test</title>


<div id="content">
  <p>
    <span class="aaa">
      abcdefghijklmn<br><span>opq</span>rstuvwxyz
    </span>
  </p>
</div>



EOF;

// create document object model
$dom = new DOMDocument();
// load html into document object model
@$dom->loadHTML($html);
// create domxpath instance
$xPath = new DOMXPath($dom);
// get all elements with a particular id and then loop through and print the href attribute
$elements = $xPath->query('//*[@id="content"]/p/span');
$nodeName = $elements->item(0)->nodeName;
// $content = $elements->item(0)->nodeValue;
$content = $dom->saveXml($elements->item(0));
$content = $dom->saveHtml($elements->item(0));
$content = preg_replace(array("#^#isU", "#{$nodeName}>$#isU"), array('', ''), $content);
echo $content;</code>

自己找到了办法。。。

<code>$content = $elements->item(0)->nodeValue;

// >> 改成 >>

$content = $dom->saveXml($elements->item(0));</code>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.