PHP Simple HTML DOM Parser

Jmnote (토론 | 기여)님의 2021년 9월 8일 (수) 00:05 판

1 개념

PHP Simple HTML DOM Parser, SimpleHTMLDom, simplehtmldom
PHP 심플 HTML DOM 파서
  • "PHP용 빠르고, 간단하고, 신뢰할 수 있는 HTML 문서 파서"
  • HTML DOM 처리기
  • MIT 라이선스
  • 이것 대신 PHP DOMXpath를 사용하자.

2 설치

Bash
Copy
composer require simplehtmldom/simplehtmldom

3 예시1: 문자열 입력

PHP
Copy
use simplehtmldom\HtmlDocument;

$html = new HtmlDocument();
$html->load('<div id="hello">Hello</div><div id="world">World</div>');
$html->find('div', 1)->class = 'bar';
$html->find('div[id=hello]', 0)->innertext = 'foo';
echo $html;
# <div id="hello">foo</div><div id="world" class="bar">World</div>

4 예시2: URL 입력

PHP
Copy
use simplehtmldom\HtmlDocument;

$html = new HtmlDocument();
$html->load_file('https://ko.wikipedia.org/');
echo $html->find('title', 0)->plaintext;
# 위키백과, 우리 모두의 백과사전

5 참고