메뉴 건너뛰기

프로그램언어

조회 수 225 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

http://simplehtmldom.sourceforge.net/

 

https://github.com/samacs/simple_html_dom

 

다운로드 링크 : http://sourceforge.net/projects/simplehtmldom/files/latest/download?source=files

 

해당 홈에 나와있는 예제입니다.

 

간단한 주석을 붙입니다.

 

//html 을 가져오고

$html = file_get_html('http://www.google.com/');            



// 모든 이미지태그를 찾아냅니다.

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

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



// 모든 a태그를 찾아내어 href속성을 뿌려줍니다.

foreach($html->find('a') as $element)                             

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



// 컨텐츠내에 텍스트들만 가져옵니다.

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



// css jquery 많이 만져보셨다면 선택자는 거의 동일합니다.

//몇번째 a태그 같은 경우도 가져올수 있구요

foreach($html->find('div.article') as $article) {

    $item['title']     = $article->find('div.title', 0)->plaintext;

    $item['intro']    = $article->find('div.intro', 0)->plaintext;

    $item['details'] = $article->find('div.details', 0)->plaintext;

    $articles[] = $item;

}



//가져오기전 해당 태그내의 텍스트도 이렇게 간단히 변경가능합니다.

$html->find('div[id=hello]', 0)->innertext = 'foo';



예제

<?php

include_once('simplehtmldom_1_5/simple_html_dom.php');

// 네이버 html을 가져온다

$html = file_get_html('http://www.naver.com/');



// 모든 이미지태그를 찾고

foreach($html->find('img') as $element) {

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

}       

 

// 모든 a태그를 찾아내어 href속성을 뿌려줍니다.

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

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

}    

?>

 


List of Articles
번호 제목 날짜 조회 수
100 파일 확장자 비교 2016.12.23 21980
99 explode - 문자열 나눔 2016.12.23 19944
98 정규표현식 매치를 수행 (preg_match) 2016.12.23 20054
97 정규 표현식 검색과 치환 (preg_replace) 2016.12.23 19012
96 $_FILES 2016.12.23 23849
95 대소문자 바꾸기 (strtoupper, strtolower) 2016.12.23 19660
94 문자열 뒤집기 (strrev) 2016.12.23 18951
93 이스케이프 함수 (htmlentities) 2016.12.23 18377
92 정규표현식 검사 도구 (ereg, eregi) 2016.12.23 18396
91 문자열의 태그를 그대로 출력 (htmlspecialchars) 2016.12.23 18072
90 preg_match (정규표현식 매치를 수행합니다) 2016.12.23 20856
89 문자열에서 태그를 제거 (strip_tags) 2016.12.23 17525
88 문자열 찾기 (strstr) 2016.12.23 18910
87 깨진 한글 체크 2016.12.23 20224
86 로그인페이지에서 온 경우/로그인한 페이지로 이동 2016.12.23 18850
85 문자열 치환 (str_replace) 2016.12.23 18813
84 문자열 추출하기 (substr) 2016.12.23 18869
83 $_SERVER 함수 2016.12.23 23944
82 링크를 걸때 http 처리방법 2016.12.23 19020
81 디렉토리 안의 파일의 내용들을 읽는 예 2016.12.23 19014
Board Pagination Prev 1 ... 8 9 10 11 12 13 14 15 16 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved