How to remove hidden tag from HTML page in PHP

1 Answer

0 votes
function remove_hidden_html_tag($html, $tag)
{
    $dom = new DOMDocument();
    libxml_use_internal_errors(true);
    $dom->loadHTML($html);

    $node = $dom->documentElement;
    if ($node === null) return html;     

    $dom_node_list = $node->getElementsByTagName($tag);

    $i = 0;
    while ($i < $dom_node_list->length)
    {    
        if (strpos($dom_node_list->item($i)->getAttribute('style'), "display:none") !== false ||
            strpos($dom_node_list->item($i)->getAttribute('style'), "visibility:hidden") !== false)
        {
            $dom_node_list->item($i)->parentNode->removeChild($dom_node_list->item($i));
            $dom_node_list = $node->getElementsByTagname($tag);
            $i=0;
        }
        else
            $i++;
    }

    $html = $dom->saveHTML();

    return $html;
}

$curl = curl_init("httt://www.websitename.com");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$html = curl_exec($curl);
curl_close($curl);
    

$html = remove_hidden_html_tag($html, 'div');

 



answered Jun 17, 2016 by avibootz
edited Jun 17, 2016 by avibootz

Related questions

1 answer 214 views
214 views asked Nov 28, 2018 by avibootz
1 answer 163 views
1 answer 297 views
1 answer 190 views
2 answers 247 views
1 answer 262 views
...