
1. HTML 문서에서 특정 태그의 내용을 추출하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$dom = new DOMDocument();
$dom->loadHTML($html);
$h1_node = $dom->getElementsByTagName('h1')->item(0);
if ($h1_node) {
echo $h1_node->nodeValue;
}
2. HTML 문서에서 특정 태그의 속성을 추출하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$dom = new DOMDocument();
$dom->loadHTML($html);
$a_node = $dom->getElementsByTagName('a')->item(0);
if ($a_node) {
echo $a_node->getAttribute('href');
}
3. HTML 문서에서 특정 노드의 자식 노드를 추출하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$dom = new DOMDocument();
$dom->loadHTML($html);
$ul_node = $dom->getElementsByTagName('ul')->item(0);
if ($ul_node) {
foreach ($ul_node->childNodes as $child_node) {
echo $child_node->nodeName . ': ' . $child_node->nodeValue . "n";
}
}
이러한 방법으로 노드 트리를 탐색하여 필요한 정보를 추출할 수 있습니다.
2025-03-21 08:13