라이브러리

[PHP] DOMElement::insertAdjacentText - 인접 텍스트 삽입




DOMElement::insertAdjacentText

PHP의 DOMElement 클래스는 HTML 문서를 다루기 위한 클래스입니다. DOMElement::insertAdjacentText 메소드는 특정 엘리먼트에 텍스트를 삽입하는 메소드입니다.

사용법


DOMElement::insertAdjacentText 메소드는 다음과 같은 형식으로 사용할 수 있습니다.

#hostingforum.kr
php

DOMElement::insertAdjacentText ( string $position , string $text ) : void



- `$position` : 삽입할 위치를 지정합니다. 다음 값이 가능합니다.
- 'beforebegin' : 엘리먼트의 시작 전
- 'afterbegin' : 엘리먼트의 시작 후
- 'beforeend' : 엘리먼트의 끝 전
- 'afterend' : 엘리먼트의 끝 후
- `$text` : 삽입할 텍스트

예제


#hostingforum.kr
php

// HTML 문서를 파싱합니다.

$html = '
원래 텍스트';

$doc = new DOMDocument();

$doc->loadHTML($html);



// 엘리먼트를 선택합니다.

$p = $doc->getElementsByTagName('p')->item(0);



// 텍스트를 삽입합니다.

$p->insertAdjacentText('beforebegin', '삽입할 텍스트 ');



// 결과를 출력합니다.

echo $doc->saveHTML();



이 예제에서는 `

원래 텍스트

` 엘리먼트의 시작 전에 '삽입할 텍스트 '를 삽입합니다.

결과


#hostingforum.kr
html


삽입할 텍스트 원래 텍스트



참고


- DOMElement::insertAdjacentText 메소드는 엘리먼트의 내용을 변경합니다. 따라서 엘리먼트의 내용을 변경하기 전에 엘리먼트의 내용을 백업하는 것이 좋습니다.
- DOMElement::insertAdjacentText 메소드는 엘리먼트의 시작 또는 끝에 텍스트를 삽입할 때 사용할 수 있습니다. 시작 또는 끝에 텍스트를 삽입할 때는 `beforebegin` 또는 `afterend` 위치를 사용합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색