between 0 and 1

PHP Simple HTML DOM Parser 링크 및 사용법 간단 정리 본문

Software Development Engineering/PHP

PHP Simple HTML DOM Parser 링크 및 사용법 간단 정리

devxpert.yoon 2018. 7. 18. 07:06
728x90
반응형

PHP Simple HTML DOM Parser


Link: http://simplehtmldom.sourceforge.net/


Usage: 



// Include the library

include('simple_html_dom.php');


// Retrieve the DOM #1 - from a given URL

$html = file_get_html('https://davidwalsh.name/');


// Retrieve thd DOM #2 - from a file or string variable

$html = str_get_html($row->value);


// Find all "A" tags and print their HREFs

foreach($html->find('a') as $e) {

    echo $e->href . '<br>';

}


// Retrieve all images and print their SRCs

foreach($html->find('img') as $e)

    echo $e->src . '<br>';

 

// count all images

count($html->find('img'));

 

// get 0th img src

$imgSrc = $html->find('img',0)->src;

 

// change 0th img src

$html->find('img',0)->src = 'https://t1.daumcdn.net/tistory_admin/static/top/tistoryLogo.gif';

 

// get updated html data

$html->__toString();


// Find all images, print their text with the "<>" included

foreach($html->find('img') as $e)

    echo $e->outertext . '<br>';


// Find the DIV tag with an id of "myId"

foreach($html->find('div#myId') as $e)

    echo $e->innertext . '<br>';


// Find all SPAN tags that have a class of "myClass"

foreach($html->find('span.myClass') as $e)

    echo $e->outertext . '<br>';


// Find all TD tags with "align=center"

foreach($html->find('td[align=center]') as $e)

    echo $e->innertext . '<br>';

    

// Extract all text from a given cell

echo $html->find('td[align="center"]', 1)->plaintext.'<br><hr>';



728x90
반응형