Scrape an H1 element on the current page in PHP

I’m currently working with WordPress. I have a hook that runs before a <title> attribute is populated with text that a user enters in the dashboard.

Now I want to set a default title of each page to equal an <h1> attribute text value on a current page. A fragment of the callback function for the hook I’m working with would look like:

Read More
if (!$seoTitle) {
    $seoTitle = '<....>';
}

return $seoTitle;

I want seoTitle to default to an <h1> element text on the current page. Is it doable? How can I achieve this?

Related posts

3 comments

  1. I’m not totally sure how you get your HTML but you could parse it with the built in DOM parser.

    <?php
    
    $html = "<!DOCTYPE html>
    <html>
    <head>
    <title>Page Title</title>
    </head>
    <body>
    
    <h1>This is a Heading one</h1>
    <p>This is a paragraph.</p>
    <h1>This is a Heading two</h1>
    <p>This is a paragraph.</p>
    <h1>This is a Heading three</h1>
    <p><a href='testwww'> This is a paragraph.</a></p>
    
    
    </body>
    </html>";
    
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    
    //If you want to get it from a website you could do the following:
    //$dom->loadHTML(file_get_contents('http://www.w3schools.com/'));
    
    // iterate through the html to get all h1 text
    foreach($dom->getElementsByTagName('h1') as $heading) {
        $h1 = $heading->nodeValue;
        echo $h1 . "<br>";
    }
    
    ?>
    
  2. Assuming you have your HTML content within a variable and doing this after the page has fully loaded please take a look at the below example:

    <?php
    $htmlContent = '<html><body><h1>HELLO</h1></body></html>'; // change this to what you need
    $seoTitle = preg_replace('/(.*)<h1>([^>]*)</h1>(.*)/is', '$2', $htmlContent);
    echo $seoTitle; // will output: HELLO
    ?>
    
  3. echo "<h1>".(string)$seoTitle."</h1>";
    

    Should work. You can also break out of the php ?> and then type regular html and then break in when you wanna echo the variable.

Comments are closed.