How to get between the div all content with tags.

Example content <div class="wrap"><a href=".."></a><span></span></div>

You need to get <a href=".."></a><span></span> that is, any content between Div

 $html = file_get_contents('http://xx.com/'); // создаем новый dom-объект $dom = new domDocument("1.0", "utf-8"); // загружаем html в объект $dom->loadHTML($html); $dom->preserveWhiteSpace = false; $counter = 0; foreach ($dom->getElementsByTagName('header') as $row) { if ($counter++ == 0) continue; echo $row->nodeValue.'<br>'; } 
  • Refine the tool with which you want to get the result. - ilyaplot
  • $ dom = new domDocument - Sauron
  • if ($ counter ++ == 0) continue; Will never be executed. - ilyaplot
  • I h3 she hid the first output head is running) - Sauron

2 answers 2

You can like this:

 <?php $html = '<div class="wrap"><a href=".."></a><span></span></div>'; preg_match('/<div class="wrap">(.*)<\/div>/s', $html, $content); echo $content[1]; ?> 
  • This method is complicated and does not always work. <div class="wrap"><div class="another"></div><a href="..."></a> Этот текст не будет выбран</div> - ilyaplot
  • @ilyaplot I solved the problem by the condition from the example. for your case, you need to use the following preg_match('/<div class="wrap">(.*)<\/div>/s', $html, $content); - ikerya pm
  • I have met many examples in which some phpQuery has saved weeks of work for developers, and regulars simply could not. For the second example, I can make a source code, which also does not work as the author expects. - ilyaplot
  • we are now fighting the enemy blindly. Do you want to look at what I am capable of or why do you put forward some tasks for me? what the author asked I did, and what you want, please, let's solve it in a new question. - ikerya
 <?php $html = '<div class="wrap"><a href=".."></a><span></span></div>'; function DOMinnerHTML(DOMNode $element) { $innerHTML = ""; $children = $element->childNodes; foreach ($children as $child) { $innerHTML .= $element->ownerDocument->saveHTML($child); } return $innerHTML; } $dom = new DOMDocument; $dom->loadHTML($html); $divs = $dom->getElementsByTagName('div'); foreach ($divs as $div) { echo DOMinnerHTML($div).PHP_EOL; // <a href=".."></a><span></span> } 
  • Such a problem as my foreach ($dom->getElementsByTagName('header') as $row) { if ($counter++ == 0) continue; echo $row->nodeValue.'<br>'; } code foreach ($dom->getElementsByTagName('header') as $row) { if ($counter++ == 0) continue; echo $row->nodeValue.'<br>'; } foreach ($dom->getElementsByTagName('header') as $row) { if ($counter++ == 0) continue; echo $row->nodeValue.'<br>'; } foreach ($dom->getElementsByTagName('header') as $row) { if ($counter++ == 0) continue; echo $row->nodeValue.'<br>'; } combine with your example? - Sauron
  • Instead of 'div' make 'header', instead of .PHP_EOL. '<br>' - ilyaplot
  • As well as the code that is on top for some reason, everything repeats 2 times, that is, 1 div displays 2 times - Sauron
  • I need to display Header from the bottom of their contents (divs). - Sauron
  • In general, I do not understand anything from what you wrote. - ilyaplot