How to Extract Heading Content (H1, H2, etc.) from an HTML String Using Regex

Headlines and headings are usually very relevant and descriptive pieces of information for any HTML page. You might want to include them into the description <meta> tag on that page. Here is a simple regular expression to extract all those headings:

preg_match_all('|<h[^>]+>(.*)</h[^>]+>|iU', $html, $headings);

Never lose a single business lead or enquiry again! Get my latest plugin Storage for Contact Form 7 now!

Buy now for only $19 →

One Comment

  1. David says:

    Saved my day thanks!

Leave a Reply