Simplehtmldom 퀵 스타트

1 개요[ | ]

simplehtmldom 퀵 스타트
simplehtmldom Quick Start
use simplehtmldom\HtmlDocument;

$html_doc = <<<EOF
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
EOF;

$html = new HtmlDocument( $html_doc );

$title = $html->find('title', 0);
var_dump( $title->outertext() );    # string(35) "<title>The Dormouse's story</title>"
var_dump( $title->tag );            # string(5) "title"
var_dump( $title->innertext() );    # string(20) "The Dormouse's story"
var_dump( $title->parent()->tag );  # string(4) "head"

$p = $html->find('p', 0);
var_dump( $p->outertext() );  # string(48) "<p class="title"><b>The Dormouse's story</b></p>"
var_dump( $p->class );        # string(5) "title"

$a = $html->find('a', 0);
var_dump( $a->outertext() );
# string(70) "<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>"

$as = $html->find('a');
foreach( $as as $a ) var_dump( $a->outertext() );
# string(70) "<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>"
# string(70) "<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a>"
# string(72) "<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>"

$link3 = $html->find('[id=link3]', 0);
var_dump( $link3->outertext() );
# string(72) "<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>"

$as = $html->find('a');
foreach( $as as $a ) var_dump( $a->href );
# string(24) "http://example.com/elsie"
# string(24) "http://example.com/lacie"
# string(25) "http://example.com/tillie"

var_dump( $html->plaintext );

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}