How to retain the html characters in nodeValue when collecting data using PHPXPath

Source: Internet
Author: User
The Code is as follows: {code ...} & amp; lt; br & amp; gt; In the content will be removed. You can retain html tags for any operations such as $ e-& amp; gt; innerHtml. 8.18 update: {code...} the code is as follows:

$html = <<
      
       
   Test
   

abcdefghijklmn
opqrstuvwxyz

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;

Content
Will be removed. html tags can be retained for any operations such as $ e-> innerHtml.

8.18 update:

$html = <<
      
       
   Test
   

abcdefghijklmn
opqrstuvwxyz

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("#^<{$nodeName}.*>#isU", "# $#isU"), array('', ''), $content);echo $content;

Reply content:

The Code is as follows:

$html = <<
      
       
   Test
   

abcdefghijklmn
opqrstuvwxyz

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;

Content
Will be removed. html tags can be retained for any operations such as $ e-> innerHtml.

8.18 update:

$html = <<
      
       
   Test
   

abcdefghijklmn
opqrstuvwxyz

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("#^<{$nodeName}.*>#isU", "# $#isU"), array('', ''), $content);echo $content;

You have found a solution...

$ Content = $ elements-> item (0)-> nodeValue; // >>>>$ content = $ dom-> saveXml ($ elements-> item (0 ));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.