M
M
mirexdoors2018-04-01 10:43:36
PHP
mirexdoors, 2018-04-01 10:43:36

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); //закрытие
  }
}

but this way the same content is written to each file. I can't figure out how to check for the parent?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mirexdoors, 2018-04-01
@mirexdoors

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='';
}

M
Mikhail Sisin, 2018-04-01
@JabbaHotep

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 = "";
  }

It also probably makes sense to check if $node->nextSibling exists before picking up its parentNode. I don't know how critical this is in PHP.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question