Based on Simple

Source: Internet
Author: User
This article introduces the use of simple HTML DOM in a detailed analysis, the need for friends to refer to the copy code code as follows:


<P> Simple Example


<?phpinclude "simple_html_dom.php"; Load simple_html_dom.php file


$html = file_get_html (' http://www.google.com/'); Get 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) {// Gets the img tag array echo $element->src. ' <br> '; Get src}//Find all links in each img tag ($dom->find (' a ') as $element) {//get array of a tags echo $element->href. ' <br> '; Get the href}</p><p>
in each a label

$html = file_get_html (' http://slashdot.org/'); Get html$dom = new Simple_html_dom (); New Simple_html_dom Object $dom->load ($html); Load html//Find all article Blocksforeach ($dom->find (' div.article ') as $article) {$item [' title '] = $article->find (' Div.title ', 0)->plaintext; PlainText get plain text $item [' intro '] = $article->find (' Div.intro ', 0)->plaintext; $item [' details '] = $article->find (' Div.details ', 0)->plaintext; $articles [] = $item;} Print_r ($articles);</p><p>}</p><p>


//Create DOM from string</p><p> $html = str_get_html (' <div id= ' Hello ' >hello</div><div Id= "World" >World</div> ");


$dom = new Simple_html_dom (); New Simple_html_dom Object </P><P> $dom->load ($html); Load HTML


$dom->find (' div ', 1)->class = ' Bar '; class = Assign value to the second div class assignment </P><P> $dom->find (' Div[id=hello] ', 0)->innertext = ' Foo '; innertext internal text </p><p>echo $dom; </p><p>//Output: <div id= "Hello" >foo</div><div id= "World" class= "bar" >world</div ></P><P> </p><p>dom Methods & Properties


Name Description


void __construct ([string $filename]) constructor, which automatically loads the content, whether text or file/url, to the file name parameter.


string plaintext Plain text


void Clear () Cleans up memory


void Load (string $content) loading content


string Save ([string $filename]) dumps the internal DOM tree back into a string. If the $filename is set, result string would 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 element's CSS selector. Returns the Nth element object if the index is set, otherwise an array object is returned. </P>


<P> 4.find Method Detailed Introduction </P><P>


Find (String $selector [, int $index])


//Find all anchors, returns a array of element objects A-tag arrays


$ret = $html->find (' a '); </p><p>//find (N) th anchor, returns element object or null if not found (zer O based) first a label


$ret = $html->find (' A ', 0); </p><p>//Find lastest Anchor, returns element object or null if not found (zero based) the last a label


$ret = $html->find (' A ',-1); </p><p>//Find all <div> with the id attribute


$ret = $html->find (' div[id] ') </p><p>//find all <div> which attribute Id=foo


$ret = $html->find (' div[id=foo] '); </P><P>


//Find all element which Id=foo


$ret = $html->find (' #foo '); </p><p>//find all element which Class=foo


$ret = $html->find ('. Foo '); </p><p>//find all element has attribute ID


$ret = $html->find (' *[id] '); </p><p>//Find all anchors and images a tag with an IMG tag array


$ret = $html->find (' A, img '); </p><p>//Find all anchors and images with the "title" attribute


$ret = $html->find (' a[title], Img[title];</p><p>


//Find all <li> in <ul>


$es = $html->find (' ul Li '); UL tags under the li tag array </p><p>//find Nested <div> tags


$es = $html->find (' div div div '); div tag under div tags under div tag array </p><p>//find all <td> in <table> which Class=hello


$es = $html->find (' Table.hello TD '); Table tab under the TD tag array </p><p>//Find all TD tags with attribite align=center in table tags


$es = $html->find (' table td[align=center] '); Method of </P><P> 5.Element


$e = $html->find ("div", 0); The methods that $e have are shown in the following table


Attribute Name Usage


$e->tag Label


$e->outertext Foreign Language


$e->innertext Text


$e->plaintext Plain text </P><P> </p><p>//Example


$html = str_get_html ("<div>foo <b>bar</b></div>");


Echo $e->tag; Returns: "Div"


Echo $e->outertext; Returns: "<div>foo <b>bar</b></div>"


Echo $e->innertext; Returns: "foo <b>bar</b>"


Echo $e->plaintext; Returns: "foo bar" </p><p>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 () after a sibling element


element$e->prev_sibling () former sibling element </P><P>


//Example


Echo $html->find ("#div1", 0)->children (1)->children (1)->children (2)->id;


//or


Echo $html->getelementbyid ("Div1")->childnodes (1)->childnodes (1)->childnodes (2)-> GetAttribute (' id ');


</P>

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.