epynic
epynic

Reputation: 1164

Scrape complete HTML tag wise

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

Upvotes: 1

Views: 136

Answers (1)

Mohammad
Mohammad

Reputation: 21499

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

Upvotes: 1

Related Questions