Simple_html_dom (1)

Source: Internet
Author: User

Create DOM from URL or file
$html = file_get_html(' http://www.google.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> ';

// 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>

Dump contents (without tags) from HTML

echo file_get_html(' http://www.google.com/'),plaintext;

Create DOM from URL
$html =file_get_html( ' http://slashdot.org/');

//Find all article blocks
foreach ($html- Find ( ' div.article ') as $article) {
     $item [ ' title ']     = $article-> Find (0)-> plaintext ;
     $item [ find ( ' Div.intro ',  0)-> plaintext ;
     $item [ ' details '] = $article-> Find ( ' Div.details ',  0)-> plaintext ;
     $articles [] = $item;
}

Print_r ($articles);

How to create HTML DOM object?

Create a DOM object
$html = new simple_html_dom();

Load HTML from a string
$html-load('
Load HTML from a URL
$html-load_file(' http://www.google.com/');

Load HTML from a HTML file
$html-load_file(' test.htm ');

How to find HTML elements?

Find AllAnchors, returns aArrayof Element objects
$ret = $html->find (‘a‘);

Find(N) thAnchor, returns element Object orNULLIf not found $ret = $html->find ( a ',   0 );

//find  lastest   anchor , returns element Object or  null  if not found  (zero based)
$ret = $html Find ( ' a ',   -1 );  

// Find all  <div>  with the  ID  attribute
$ret = $html- >find ( ' Div[id] ');

//Find all  <div>  which attribute  Id=foo
$ret = $html->find ( ' Div[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 have attribute  id
$ret = $html->find ( Span class= "var" > ' *[id] ');  

//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 <li> in <ul> 
$es = $html->find ( ul Li ');

//Find Nested <div> tags
$es = $html->find ( Span class= "var", " div div div ');  

//Find all <td> in < table> 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 <li> in <ul> 
foreach ($html->find ( ul ') as $ul)  
{
       foreach ($ul- >find ( ' Li ') as $li)  
       {
             //do Something ...
      }
}

//Find first <li> in first <ul> 
$e = $html->find ( ' ul ',   0 )->find ( ' Li ',   0 );

Find All text blocks
$es = $html->find ('text');

Find All COMMENT (<!--...-->) blocks
$es = $html->find ('comment');

Supports These operators in attribute selectors:

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

Simple_html_dom (1)

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.