You can select values between two nodes in
DOM Crawler
by using the
filterXPath
method
with an XPath expression that selects the nodes between the two nodes you want to use as anchors.
Here is some sample code that prints the text content of all the nodes between the h1
and h2
nodes:
use Symfony\Component\DomCrawler\Crawler;
$html = <<<EOD
<div>
<h1>Header 1</h1>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<h2>Header 2</h2>
<p>Paragraph 3</p>
</div>
EOD;
// Load the HTML document
$crawler = new Crawler($html);
// Find all nodes between the h1 and h2 elements
$nodesBetweenHeadings = $crawler->filterXPath('//h1/
following-sibling::h2/
preceding-sibling::*[
preceding-sibling::h1
]');
// Loop over the nodes and print their text content
foreach ($nodesBetweenHeadings as $node) {
echo $node->textContent . PHP_EOL;
}
The XPath expression used above can be read like this:
//h1
: Go to theh1
tag/following-sibling::h2
: Go to the siblingh2
tag/preceding-sibling::*[preceding-sibling::h1]
: Find all the preceding siblings of the h2 tag that have anh1
tag as a preceding sibling (*
matches all tags)