Я создал скрипт php в PHP Dom, где несколько html-файлов очищаются для поиска всех P-тегов, которые содержат определенный класс.
Затем я хочу получить значения внутри этих тегов p и создать неупорядоченный список в PHP Dom.
Моя проблема в том, что я могу получить значения и вывести их все на страницу, когда я пытаюсь создатьElements и добавить каждое значение в свой собственный тег LI, мои результаты возвращают только последний элемент в списке.Я надеюсь, что в этом есть смысл.Вот код:
$dom = new DOMDocument();
$dom->formatOutput = true;
$dom->preservewhiteSpace = false;
//looping through an array
foreach ($pages as $page) {
foreach ($page['pageContent'] as $listlinks) {
$dom->loadHTMLFile($theurl . 'content_id_' . $listlinks['content'] . '.html');
//create the xPath object after loading the html source, otherwise the query won't work:/
$xPath = new DOMXPath($dom);
//get the p nodes in a DOMNodeList that has class"content_header_type_2":
$nodeList = $xPath->query("//p[@class='content_header_type_2']");
//create a new DOMDocument and add a ul element:
$newDom = new DOMDocument();
$ul = $newDom->createElement('ul');
$newDom->appendChild($ul);
// append all nodes from $nodeList to the new dom, as children of $ul:
foreach ($nodeList as $domElement) {
$domNode = $newDom->importNode($domElement, true);
echo $domNode->nodeValue . '<br>'; //This gives the entire list
$li = $newDom->createElement('li', $domNode->nodeValue); //This gives the last value in the list
$ul->appendChild($li);
}
}
};
$output = $newDom ->saveHTML();
echo $output;