라이브러리

[PHP] DOMNodeList::count - 목록에 있는 노드 수를 가져옵니다.




DOMNodeList::count


DOMNodeList::count는 DOMNodeList 객체의 요소 개수를 반환하는 메서드입니다. DOMNodeList는 HTML 또는 XML 문서의 요소 노드의 목록을 나타내는 객체입니다.

# 사용법


DOMNodeList::count 메서드는 다음과 같이 사용할 수 있습니다.

#hostingforum.kr
php

$nodeList = $document->getElementsByTagName('div');

$count = $nodeList->count;



echo $count; // 요소 개수 출력



# 예제


다음 예제에서는 HTML 문서에서 `
` 요소의 개수를 출력하는 코드를 보여줍니다.

#hostingforum.kr
php

// HTML 문서를 파싱하는 도큐먼트 객체를 생성합니다.

$document = new DOMDocument();

$document->loadHTML('
paragraph 1
paragraph 2');



// DOMNodeList 객체를 생성합니다.

$nodeList = $document->getElementsByTagName('div');



// 요소 개수를 출력합니다.

$count = $nodeList->count;

echo "div 요소 개수: $count
";



// 각 요소를 출력합니다.

foreach ($nodeList as $node) {

    echo "  - " . $node->nodeValue . "
";

}



# 결과


#hostingforum.kr


div 요소 개수: 2

  - paragraph 1

  - paragraph 2



# 참고


DOMNodeList::count 메서드는 DOMNodeList 객체의 요소 개수를 반환합니다. 이 메서드는 DOMNodeList 객체의 요소 개수를 확인할 때 사용할 수 있습니다.

  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 610 페이지

검색

게시물 검색