How to get a div via PHP?

17,291

Solution 1

Once you have loaded the document to a DOMDocument instance, you can use XPath queries on it -- which might be easier than going yourself through the DOM.

For that, you can use the DOMXpath class.


For example, you should be able to do something like this :

$dom = new DOMDocument();
$dom->loadHTML($html);

$xpath = new DOMXPath($dom);
$tags = $xpath->query('//div[@class="text"]');
foreach ($tags as $tag) {
    var_dump($tag->textContent);
}


(Not tested, so you might need to adapt the XPath query a bit...)

Solution 2

Personally, I like Simple HTML Dom Parser.

include "lib.simple_html_dom.php"

$html = file_get_html('http://scrapeyoursite.com');
$html->find('div.text')->plaintext;

Pretty simple, huh? It accommodates selectors like jQuery :)

Share:
17,291
trrrrrrm
Author by

trrrrrrm

Updated on June 29, 2022

Comments

  • trrrrrrm
    trrrrrrm almost 2 years

    I get a page using file_get_contents from a remote server, but I want to filter that page and get a DIV from it that has class "text" using PHP. I started with DOMDocument but I'm lost now.

    Any help?

    $file = file_get_contents("xx");
    $elements = new DOMDocument();
    $elements->loadHTML($file);
    foreach ($elements as $element) {
        if( !is_null($element->attributes)) {
            foreach ($element->attributes as $attrName => $attrNode) {
                if( $attrName == "class" && $attrNode== "text") {
                    echo $element;
                }
            }
        }
    }
    
  • trrrrrrm
    trrrrrrm about 14 years
    a warning still produced Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: htmlParseEntityRef: expecting ';' in Entity, line: 5 don't know why
  • Pascal MARTIN
    Pascal MARTIN about 14 years
    Maybe a problem in your document ? Still, if it's loaded properly, you could use the @ operator (see fr.php.net/manual/en/language.operators.errorcontrol.php ) to silence that error. Note : using that operator is not a good pratice, and you should it as rarely as possible !
  • teutara
    teutara almost 11 years
    Thanks @maček.. It might have been ages that you posted this, but I just needed something just like this. Cheers!