SimplehtmldomDocapi help document

Source: Internet
Author: User
Simple_html_dom.php is a powerful html xml analysis tool like jquery, and a PHP tool for HTML processing. it is similar to jquery. For more information, see API Reference

Helper functions
Object str_get_html (string $ content) Creates a DOM object from a string.
Object file_get_html (string $ filename) Creates a DOM object from a file or a URL.

DOM methods & properties

Stringplaintext Returns the contents extracted from HTML.
Voidclear () Clean up memory.
Voidload (string $ content) Load contents from a string.
Stringsave ([string $ filename]) Dumps the internal DOM tree back into a string. If the $ filename is set, result string will save to file.
Voidload_file (string $ filename) Load contents from a file or a URL.
Voidset_callback (string $ function_name) Set a callback function.
Mixedfind (string $ selector [, int $ index]) Find elements by the CSS selector. Returns the Nth element object if index is set, otherwise return an array of object.

Element methods & properties

String [attribute] Read or write element's attribure value.
Stringtag Read or write the tag name of element.
Stringoutertext Read or write the outer HTML text of element.
Stringinnertext Read or write the inner HTML text of element.
Stringplaintext Read or write the plain text of element.
Mixedfind (string $ selector [, int $ index]) Find children by the CSS selector. Returns the Nth element object if index is set, otherwise, return an array of object.

DOM traversing

Mixed $ e-> children ([int $ index]) Returns the Nth child object if index is set, otherwise return an array of children.
Element $ e-> parent () Returns the parent of element.
Element $ e-> first_child () Returns the first child of element, or null if not found.
Element $ e-> last_child () Returns the last child of element, or null if not found.
Element $ e-> next_sibling () Returns the next sibling of element, or null if not found.
Element $ e-> prev_sibling () Returns the previous sibling of element, or null if not found.
Camel naming convertions You can also call methods with W3C STANDARD camel naming convertions.


String $ e-> getAttribute ($ name) string $ e-> attribute
Void $ e-> setAttribute ($ name, $ value) void $ value = $ e-> attribute
Bool $ e-> hasAttribute ($ name) boolisset ($ e-> attribute)
Void $ e-> removeAttribute ($ name) void $ e-> attribute = null
Element $ e-> getElementById ($ id) mixed $ e-> find ("# $ id", 0)
Mixed $ e-> getElementsById ($ id [, $ index]) mixed $ e-> find ("# $ id" [, int $ index])
Element $ e-> getElementByTagName ($ name) mixed $ e-> find ($ name, 0)
Mixed $ e-> getElementsByTagName ($ name [, $ index]) mixed $ e-> find ($ name [, int $ index])
Element $ e-> parentNode () element $ e-> parent ()
Mixed $ e-> childNodes ([$ index]) mixed $ e-> children ([int $ index])
Element $ e-> firstChild () element $ e-> first_child ()
Element $ e-> lastChild () element $ e-> last_child ()
Element $ e-> nextSibling () element $ e-> next_sibling ()
Element $ e-> previussibling () element $ e-> prev_sibling ()





// Create a DOM object from a string
$ Html = str_get_html ('Hello!');

// Create a DOM object from a URL
$ Html = file_get_html ('http: // www.google.com /');

// Create a DOM object from a HTML file
$ Html = file_get_html('test.htm ');



// Create a DOM object
$ Html = new simple_html_dom ();

// Load HTML from a string
$ Html-> load ('Hello!');

// Load HTML from a URL
$ Html-> load_file ('http: // www.google.com /');

// Load HTML from a HTML file
$ Html-> load_file('test.htm ');


// Find all anchors, returns a array of element objects
$ Ret = $ html-> find ('A ');

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

// Find all

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

// Find all

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

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


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

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

// Find all anchors and images
$ 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 ');

      // Find Nested

      Tags
      $ Es = $ html-> find ('P P ');

      // Find allIn







    • In

        Foreach ($ html-> find ('Ul ') as $ ul)
        {
        Foreach ($ ul-> find ('Lil') as $ li)
        {
        // Do something...
        }
        }

        // Find first
      • In first

          $ E = $ html-> find ('Ul ', 0)-> find ('Lil', 0 );

          Supports these operators in attribute selectors:


          [Attribute] Matches elements that have the specified attribute.
          [Attribute = value] Matches elements that have the specified attribute with a certain value.
          [Attribute! = Value] Matches elements that don't have the specified attribute with a certain value.
          [Attribute ^ = value] Matches elements that have the specified attribute and it starts with a certain value.
          [Attribute $ = value] Matches elements that have the specified attribute and it ends with a certain value.
          [Attribute * = value] Matches elements that have the specified attribute and it contains a certain value.

          // Find all text blocks
          $ Es = $ html-> find ('text ');

          // Find all comment ( ) Blocks
          $ Es = $ html-> find ('Comment ');

          // Get a attribute (If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)
          $ Value = $ e-> href;

          // Set a attribute (If the attribute is non-value attribute (eg. checked, selected...), set it's value as true or false)
          $ E-> href = 'My link ';

          // Remove a attribute, set it's value as null!
          $ E-> href = null;

          // Determine whether a attribute exist?
          If (isset ($ e-> href ))
          Echo 'href exist! ';

          // Example
          $ Html = str_get_html ("

          FooBar

          ");
          $ E = $ html-> find ("p", 0 );

          Echo $ e-> tag; // Returns: "p"
          Echo $ e-> outertext; // Returns :"

          FooBar

          "
          Echo $ e-> innertext; // Returns: "foo Bar"
          Echo $ e-> plaintext; // Returns: "foo bar"


          $ E-> tag Read or write the tag name of element.
          $ E-> outertext Read or write the outer HTML text of element.
          $ E-> innertext Read or write the inner HTML text of element.
          $ E-> plaintext Read or write the plain text of element.

          // Extract contents from HTML
          Echo $ html-> plaintext;

          // Wrap a element
          $ E-> outertext ='

          '. $ E-> outertext .'

          ';

          // Remove a element, set it's outertext as an empty string
          $ E-> outertext = '';

          // Append a element
          $ E-> outertext = $ e-> outertext .'

          Foo

          ';

          // Insert a element
          $ E-> outertext ='

          Foo

          '. $ E-> outertext;

          // If you are not so familiar with html dom, check this link to learn more...

          // 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 ');
          You can also call methods with Camel naming convertions.

          Mixed $ e-> children ([int $ index]) Returns the Nth child object if index is set, otherwise return an array of children.
          Element $ e-> parent () Returns the parent of element.
          Element $ e-> first_child () Returns the first child of element, or null if not found.
          Element $ e-> last_child () Returns the last child of element, or null if not found.
          Element $ e-> next_sibling () Returns the next sibling of element, or null if not found.
          Element $ e-> prev_sibling () Returns the previous sibling of element, or null if not found.

          // Dumps the internal DOM tree back into string
          $ Str = $ html;

          // Print it!
          Echo $ html;

          // Dumps the internal DOM tree back into string
          $ Str = $ html-> save ();

          // Dumps the internal DOM tree back into a file
          $ Html-> save('result.htm ');

          // Write a function with parameter "$ element"
          Function my_callback ($ element ){
          // Hide allTags
          If ($ element-> tag = 'B ')
          $ Element-> outertext = '';
          }

          // Register the callback function with it's function name
          $ Html-> set_callback ('My _ callback ');

          // Callback function will be invoked while dumping
          Echo $ html;

    • Which class = hello$ Es = $ html-> find ('table. hello td ');// Find all td tags with attribite align = center in table tags$ Es = $ html-> find (''table td [align = center] ');// Find all
  • 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.