Simple_html_dom-based usage summary _ PHP Tutorial

Source: Internet
Author: User
Summary based on simple_html_dom. Copy the code as follows: what is a simple example of P? Phpincludesimple_html_dom.php; load the simple_html_dom.php file $ htmlfile_get_html (www.google.com); get html $ domnew The code is as follows:


Simple example
$ Html = file_get_html ('http: // www.google.com/'); // Obtain html $ dom = new simple_html_dom (); // new simple_html_dom object $ dom-> load ($ html) // load html // Find all images foreach ($ dom-> find ('IMG ') as $ element) {// Obtain the img tag array echo $ element-> src.'
'; // Get src} in each img tag // Find all links foreach ($ dom-> find ('A') as $ element) {// Obtain the array echo $ element-> href of the tag.'
'; // Obtain the href in each a tag}


$ Html = file_get_html ('http: // slashdot.org/'); // Obtain html $ dom = new simple_html_dom (); // new simple_html_dom object $ dom-> load ($ html); // load html // Find all article blocksforeach ($ dom-> find ('P. article ') as $ article) {$ item ['title'] = $ article-> find ('P. title ', 0)-> plaintext; // plaintext get plain text $ item ['Intro'] = $ article-> find ('P. intro ', 0)-> plaintext; $ item ['Details'] = $ article-> find ('P. details ', 0)-> plaintext; $ articles [] = $ item;} print_r ($ articles );

}


// Create DOM from string

$ Html = str_get_html ('

Hello

World

');
$ Dom = new simple_html_dom (); // new simple_html_dom object

$ Dom-> load ($ html); // load html
$ Dom-> find ('P', 1)-> class = 'bar'; // class = assign a value to the class of the second p

$ Dom-> find ('P [id = hello] ', 0)-> innertext = 'foo'; // innertext internal text

Echo $ dom;

// Output:

Foo

World

DOM methods & properties
Name Description
Void _ construct ([string $ filename]) constructor automatically loads the file name parameters, whether it is text or file/url.
String plaintext plain text
Void clear () clears memory
Void load (string $ content) loads content
String save ([string $ filename]) Dumps the internal DOM tree back into a string. If the $ filename is set, result string will save to file.
Void load_file (string $ filename) Load contents from a file or a URL.
Void set_callback (string $ function_name) sets a callback function.
Mixed find (string $ selector [, int $ index]) finds the CSS selector of the element. Returns the nth element object. if the index is set, an array object is returned.


4. detailed description of the find method


Find (string $ selector [, int $ index])
// Find all anchors, returns a array of element objects a tag array
$ Ret = $ html-> find ('A ');

// Find (N) th anchor, returns element object or null if not found (zero based) first a tag
$ Ret = $ html-> find ('A', 0 );

// Find lastest anchor, returns element object or null if not found (zero based) the last a tag
$ Ret = $ html-> find ('A',-1 );

// Find all

With the id attribute
$ Ret = $ html-> find ('P [id] ');

// Find all

Which attribute id = foo
$ Ret = $ html-> find ('P [id = foo] ');


// Find all element which id = foo
$ Ret = $ html-> find ('# Foo ');

// Find all element which class = foo
$ Ret = $ html-> find ('. Foo ');

// Find all element has attribute id
$ Ret = $ html-> find ('* [id]');

// Find all anchors and images a tag and img tag array
$ Ret = $ html-> find ('a, img ');

// Find all anchors and images with the "title" attribute
$ Ret = $ html-> find ('a [title], img [title] ');


// Find all

  • In

      $ Es = $ html-> find ('Ul li'); array of li tags under ul tags

      // Find Nested

      Tags
      $ Es = $ html-> find ('P P'); p tag array under p tag

      // Find allIn


      // Find all td tags with attribite align = center in table tags
      $ Es = $ html-> find (''table td [align = center] ');

      5. Element method
      $ E = $ html-> find ("p", 0); // The methods owned by $ e are shown in the following table.
      Attribute Name Usage
      $ E-> tag
      $ E-> outertext
      $ E-> innertext
      $ E-> plaintext plain text

      // Example
      $ Html = str_get_html ("



      FooBar


      Bar

      6. DOM traversing method
      Method Description
      Mixed $ e-> children ([int $ index]) child element
      Element $ e-> parent () parent element
      Element $ e-> first_child () first child element
      Element $ e-> last_child () last child element
      Element $ e-> next_sibling () is the last sibling element.
      Element $ e-> prev_sibling ()


      // Example
      Echo $ html-> find ("# p1", 0)-> children (1)-> children (1)-> children (2)-> id;
      // Or
      Echo $ html-> getElementById ("p1")-> childNodes (1)-> childNodes (1)-> childNodes (2)-> getAttribute ('id ');


      FooBar

      The http://www.bkjia.com/PHPjc/327967.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327967.htmlTechArticle code is as follows: P simple example? Phpinclude "simple_html_dom.php"; // load the simple_html_dom.php file $ html = file_get_html ('http: // www.google.com/'); // Obtain html $ dom = new...

      Which class = hello$ Es = $ html-> find ('table. hello td '); td tag array under table tags ");Echo $ e-> tag; // Returns: "p"Echo $ e-> outertext; // Returns :""Echo $ e-> innertext; // Returns: "foo"Echo $ e-> plaintext; // Returns: "foo bar"
  • 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.