라이브러리

[PHP] DOMElement::getAttributeNames - 속성 이름 가져오기




DOMElement::getAttributeNames()

`DOMElement::getAttributeNames()`는 DOMElement 객체의 모든 속성을 반환하는 메서드입니다. 속성은 HTML 문서의 태그의 속성으로, 태그의 이름과 값으로 구성됩니다.

사용 방법

`getAttributeNames()` 메서드는 DOMNodeList 객체를 반환합니다. DOMNodeList 객체는 DOMElement 객체의 모든 속성을 포함하는 NodeList 객체입니다.

#hostingforum.kr
php

$doc = new DOMDocument();

$doc->loadHTML('
Hello World!'); $element = $doc->getElementsByTagName('div')->item(0); $attributeNames = $element->getAttributeNames(); foreach ($attributeNames as $name) { echo $name . " "; }


위 예제에서, `$element` 객체는 `
Hello World!
` 태그를 나타냅니다. `$attributeNames` 변수는 `$element` 객체의 모든 속성을 포함하는 NodeList 객체입니다. `foreach` 루프를 사용하여 NodeList 객체의 각 속성 이름을 출력합니다.

결과

#hostingforum.kr


id

class



위 예제의 결과는 `$element` 객체의 속성 이름인 `id`와 `class`입니다.

속성 이름과 속성 값

속성 이름과 속성 값을 얻으려면 `getAttribute()` 메서드를 사용할 수 있습니다.

#hostingforum.kr
php

$doc = new DOMDocument();

$doc->loadHTML('
Hello World!'); $element = $doc->getElementsByTagName('div')->item(0); $attributeNames = $element->getAttributeNames(); foreach ($attributeNames as $name) { echo $name . ": " . $element->getAttribute($name) . " "; }


위 예제에서, `$element` 객체의 속성 이름과 속성 값을 출력합니다.

결과

#hostingforum.kr


id: test

class: test-class



위 예제의 결과는 `$element` 객체의 속성 이름과 속성 값입니다.

속성 이름과 속성 값을 동시에 얻기

속성 이름과 속성 값을 동시에 얻으려면 `getAttributeNames()` 메서드를 사용할 수 있습니다.

#hostingforum.kr
php

$doc = new DOMDocument();

$doc->loadHTML('
Hello World!'); $element = $doc->getElementsByTagName('div')->item(0); $attributes = array(); foreach ($element->getAttributeNames() as $name) { $attributes[$name] = $element->getAttribute($name); } print_r($attributes);


위 예제에서, `$element` 객체의 속성 이름과 속성 값을 동시에 출력합니다.

결과

#hostingforum.kr
php

Array

(

    [id] => test

    [class] => test-class

)



위 예제의 결과는 `$element` 객체의 속성 이름과 속성 값입니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색