내가 HTML 문서에서 임의의 조각을 추출하는 DOMDocument 클래스와 xpath를 사용하는 것이 좋습니다, 정규 표현식 기반 솔루션 (등, 이상한 장소에서 추가 속성, 공백 추가) 입력 변경에 꽤 취성 그것은 더 복잡한에 대한 읽을 수 있어요 시나리오.
$html = '<html><body><div class="description"><p>some text here</p></div></body></html>';
// or you could fetch external sites
// $html = file_get_contents('http://example.com');
$doc = new DOMDocument();
// prevent parsing errors (frequent with HTML)
libxml_use_internal_errors(true);
$doc->loadHTML($html);
// enable back parsing errors as the HTML document is already parsed and stored in $doc
libxml_use_internal_errors(false);
$xpath = new DOMXpath($doc);
foreach ($xpath->query('//div[@class="description"]') as $el) {
var_dump($el->textContent);
}
답변 해 주셔서 감사합니다. 아무런 차이가없는 것으로 보입니다. –
단일 행 모드에 대한 설명이 잘못되었습니다. 기본적으로'.'는 줄 분리 기호 문자와 일치하지 않지만 단 일행 모드를 켜면 모든 것이 일치합니다. 행 분리 기호로 간주되는 것은 정규 표현식 및 설정에 따라 다르지만 항상 줄 바꿈 문자 ('\ n', LF)를 포함합니다. (일부 맛은 단선 대신 DOTALL 모드라고 부릅니다. IMO가 훨씬 좋습니다.) –
Alan, 당신은 틀림 없습니다. 오해하지 않도록 내 대답을 업데이트했습니다. – kingcoyote