Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,995 questions

51,940 answers

573 users

How to get the web page h1 tag content in PHP

1 Answer

0 votes
function get_html($url) {
    $handle = curl_init();
            
    curl_setopt($handle, CURLOPT_HTTPGET, true);
    curl_setopt($handle, CURLOPT_HEADER, true);
    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
            
    $output = curl_exec($handle);
    
    curl_close($handle);
            
    $separator = "\r\n\r\n";
    $header = substr($output, 0, strpos($output, $separator));
       
    $body_start = strlen($header) + strlen($separator);
    $html = substr($output, $body_start, strlen($output) - $body_start);
        
    return $html;
}
    
    
$url = "https://www.collectivesolver.com/"; 
    
$html = get_html($url);
 
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
 
$h1Tags = $doc->getElementsByTagName('h1');
if (count($h1Tags) > 0) {
    $h1 = $h1Tags[0]->nodeValue;
}
    
echo $h1;

    
    
/*
run:
    
Recent questions and answers
    
*/

 



answered Sep 14, 2019 by avibootz

Related questions

2 answers 232 views
1 answer 248 views
1 answer 190 views
1 answer 165 views
165 views asked Sep 13, 2019 by avibootz
...