BottledTurtles BottledTurtles - 5 months ago 14
PHP Question

How to get html codes between two tags

Here's HTML Codes:

...
<div class="body" style="text-align: justify;padding: 10px;">
<div>1</div><div>one</div><div>2</div><div>two</div>
</div>
...


I want to get innerHTML of a div tag which has
class="body"

Answer

Use XPath selector for tasks similar to this.

$dom = new DomDocument();
$dom->load($filePath);
$finder = new DomXPath($dom);
$classname="body";
$nodes = $finder->query("//*[contains(@class, '$classname')]");