PHP Simple html dom:Get Any Thing From Any Html Page

Discussion in 'PHP' started by astkboy2008, Dec 24, 2009.

  1. #1
    (Simple html dom 1.11) is A simple PHP HTML DOM parser written in PHP5+, supports invalid HTML, and provides a very easy way to handle HTML elements.
    And it

    • Supports invalid HTML.
    • Find tags on an HTML page with selectors just like jQuery.
    • Extract contents from HTML in a single line.

    Quick Start

    How to get HTML elements?
    // Create DOM from URL or file
    $html = file_get_html('http://www.jooria.com/');
    
    // Find all images
    foreach($html->find('img') as $element)
           echo $element->src . '<br>';
    
    // Find all links
    foreach($html->find('a') as $element)
           echo $element->href . '<br>'; 
    
    PHP:
    How to modify HTML elements?
    // Create DOM from string
    $html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>');
    
    $html->find('div', 1)->class = 'bar';
    
    $html->find('div[id=hello]', 0)->innertext = 'foo';
    
    echo $html; // Output: <div id="hello">foo</div><div id="world" class="bar">World</div>
    
    PHP:

    Extract contents from HTML

    // Dump contents (without tags) from HTML
    echo file_get_html('http://www.jooria.com/')->plaintext;
    
    PHP:

     
    astkboy2008, Dec 24, 2009 IP
    crivion and Gray Fox like this.
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    hey this is something that it's usefull +rep
     
    crivion, Dec 25, 2009 IP
  3. astkboy2008

    astkboy2008 Peon

    Messages:
    211
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks my dear for the +rep and the replay
     
    astkboy2008, Dec 26, 2009 IP