Suppose I have an HTML page as
<p> Some text here </p>
<p> Some other text here </p>
<h1> Title 1 </h1>
<p> Another text here </p>
<p> Some random text here </p>
<h1> Title 2 </h1>
<p> Some text here </p>
<p> Some other text here </p>
<h1>..<h1>
Is it possible to scrape the content's tag by tag
if (<h1>)
then do something
if (<p>)
then do something else
For each of the tags
The php getElementsByTagName()
select element by name of tag. If you put *
in function parameter, it return all elements.
$dom = new DOMDocument();
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('*') as $element){
if ($element->tagName == "h1")
// do something
if ($element->tagName == "p")
// do something
}
Check result in demo