Answer the question
In order to leave comments, you need to log in
How to parse correctly?
There are several divs on the page with the class "issue" and id like "id=issue4" you need to save the contents of each div in a separate file. I do it like this:
$nodes = $dom->execute('div.issue>div'); //выборка потомков <div class="issue">
$page ='';
foreach ($nodes as $node) {
$rawContent = $node->C14N();
$parent = $node->parentNode;
$content =html_entity_decode($rawContent);//преобразуем юникод в html
if ($parent == $node->nextSibling->parentNode) {
$page .= $content;
$id =$parent->getAttribute('id'); //значение id
$num = substr($id, 5); //обрезаем "issue"
$fp = fopen($catalog . '/' . $num . '.html', "w"); //создание файла
fwrite($fp, $page); //запись
fclose($fp); //закрытие
}
}
Answer the question
In order to leave comments, you need to log in
Solved the problem by selecting the parent
$nodes = $dom->execute('div.issue'); //выборка <div class="issue">
foreach ($nodes as $node) {
$rawContent = $node->childNodes;
foreach ($rawContent as $value) {
$page .=$value->C14N();
}
$parent = $node->parentNode;
$content =html_entity_decode($page);//преобразуем юникод в html
$id = $node->getAttribute('id'); //значение id
$num = substr($id, 5); //обрезаем "issue"
$fp = fopen($catalog . '/' . $num . '.html', "w"); //создание файла
fwrite($fp, $content); //запись
fclose($fp); //закрытие
$page='';
}
I can’t say that I understand PHP well, but judging by the logic, you have a strange condition ($parent == $node->nextSibling->parentNode), in my opinion, else is clearly missing
if ($parent == $node->nextSibling->parentNode) {
$page .= $content;
} else {
$id =$parent->getAttribute('id'); //значение id
$num = substr($id, 5); //обрезаем "issue"
$fp = fopen($catalog . '/' . $num . '.html', "w"); //создание файла
fwrite($fp, $page); //запись
fclose($fp); //закрытие
$page = "";
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question