phphtmlweb-scrapingpre

PHP Scrape HTML Between <pre> tags


I'm having trouble with finding out how to scrape HTML content from only inside

 and 
tags with PHP5.

I want to take an example of the following document, and take the 2 (or more pre tag areas, its dynamic) and shove it into an array.

blablabla
<pre>save
this
really</pre>
not this
<pre>save this too
really
</pre>
but not this

how do i shove the area between the pre tags of a html file on another server into an array.


Solution

  • I recommend using xpath

    $doc = new DOMDocument();
    $doc->loadHTML($html);
    $xpath = new DomXpath($doc);
    
    $pre_tags = array();
    foreach($xpath->query('//pre') as $node){
        $pre_tags[] = $node->nodeValue;
    }