問題:
您擁有包含標題和元素的HTML內容常規文本。您的目標是將具有指定類別的元素中的文字(標題為“Heading1-H”,文字為“Normal-H”)提取到兩個單獨的陣列中:$heading 和 $content。
解決方案:
使用PHP DOM 和XPath
PHP DOM(文檔物件模型)和XPath(XPath語言)為此任務提供了強大的解決方案。這是實作:
$test = <<<HTML <p class="Heading1-P"> <span class="Heading1-H">Chapter 1</span> </p> <p class="Normal-P"> <span class="Normal-H">This is chapter 1</span> </p> <p class="Heading1-P"> <span class="Heading1-H">Chapter 2</span> </p> <p class="Normal-P"> <span class="Normal-H">This is chapter 2</span> </p> <p class="Heading1-P"> <span class="Heading1-H">Chapter 3</span> </p> <p class="Normal-P"> <span class="Normal-H">This is chapter 3</span> </p> HTML; $dom = new DOMDocument(); $dom->loadHTML($test); $xpath = new DOMXPath($dom); $heading = parseToArray($xpath, 'Heading1-H'); $content = parseToArray($xpath, 'Normal-H'); var_dump($heading); echo "<br/>"; var_dump($content); echo "<br/>"; function parseToArray(DOMXPath $xpath, string $class): array { $xpathquery = "//*[@class='$class']"; $elements = $xpath->query($xpathquery); $resultarray = []; foreach ($elements as $element) { $nodes = $element->childNodes; foreach ($nodes as $node) { $resultarray[] = $node->nodeValue; } } return $resultarray; }
輸出:
array(3) { [0] => string(8) "Chapter 1" [1] => string(8) "Chapter 2" [2] => string(8) "Chapter 3" } <br/> array(3) { [0] => string(15) "This is chapter 1" [1] => string(15) "This is chapter 2" [2] => string(15) "This is chapter 3" } <br/>
以上是如何使用 PHP 將具有不同類別的特定 HTML 元素中的文字提取到單獨的陣列中?的詳細內容。更多資訊請關注PHP中文網其他相關文章!