依您的需求量身訂做,提供投資型、保障型
退休型等多元化保險商品,專業有保障
招牌專業居家清潔
免費到府估價,清潔養護馬上搞定

首頁  •  tw 論壇 • 程式設計討論     • 

[php] Simple HTML DOM

房東:大頭
發表時間:2011-03-16
[檢舉]


// 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>\';








How to create HTML DOM object?


Top





// Create a DOM object from a string

$html = str_get_html(\'<html><body>Hello!</body></html>\');



// 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(\'<html><body>Hello!</body></html>\');



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


Top





// Find all anchors, returns a array of element objects

$ret = $html->find(\'a\');



// Find (N)th anchor, returns element object or null if not found
(zero based)

$ret = $html->find(\'a\', 0);



// Find all <div> which attribute id=foo


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



// Find all <div> with the id attribute


$ret = $html->find(\'div[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]\');




Supports these operators in attribute selectors:



































Filter Description
[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 <li> in <ul>

$es = $html->find(\'ul li\');



// Find Nested <div>
tags

$es = $html->find(\'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 text blocks

$es = $html->find(\'text\');



// Find all comment (<!--...-->) blocks


$es = $html->find(\'comment\');




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







  • 贊助網站       

    廣利不動產-新板特區指名度最高、值得您信賴的好房仲
    您的托付,廣利用心為您服務
    廣利不動產-板橋在地生根最實在--新板特區指名度最高、值得您信賴的好房仲
    完整房訊,房屋、店面熱門精選物件,廣利不動產 優質仲介,房屋租賃、買賣資訊透明,交易真安心!

  • 1 樓住戶:小可
    發表時間:2011-03-16
    [檢舉]

    // 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 . '
    ';

    // Find all links
    foreach($html->find('a') as $element)
    echo $element->href . '
    ';

    ?>


    $url='http://t.qq.com';
    $lines_array=file($url);
    $lines_string=implode('',$lines_array);
    echo htmlspecialchars($lines_string);
    ?>

    $url='http://t.qq.com';
    $lines_string=file_get_contents($url);
    echo htmlspecialchars($lines_string);
    ?>



    $url='http://t.qq.com';
    $handle=fopen($url,"rb");
    $lines_string="";
    do{
    $data=fread($handle,1024);
    if(strlen($data)==0){break;}
    $lines_string.=$data;
    }while(true);
    fclose($handle);
    echo htmlspecialchars($lines_string);
    ?>



    $url='http://t.qq.com';
    $ch=curl_init();
    $timeout=5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $lines_string=curl_exec($ch);
    curl_close($ch);
    echo htmlspecialchars($lines_string);
    ?>


    $fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr);
    if (!$fp) {
    echo "ERROR: $errno - $errstr
    \n";
    } else {
    fwrite($fp, "\n");
    echo fread($fp, 26);
    fclose($fp);
    }
    ?>


    $url='http://t.qq.com';
    $lines_string=file_get_contents($url);
    eregi('(.*)',$lines_string,$title);
    echo htmlspecialchars($title[0]);
    ?>



    $url='http://www.yahoo.com.tw';
    $html=new DOMDocument();
    $html->loadHTMLFile($url);
    $title=$html->getElementsByTagName('title');
    echo $title->item(0)->nodeValue;
    ?>




    $url='http://www.psper.tw';
    include_once('../simplehtmldom/simple_html_dom.php');
    $html=file_get_html($url);
    $title=$html->find('title');
    echo $title[0]->plaintext;
    ?>




     共 1 人回應  選擇頁數 【第1 頁】 

    姓名:
    佈告內容: