How do I pattern match all content between<h1>and </h1> tags?

I'm trying to extract all content from an html page that exists between <h1> and </h1> tag. My code however pulls content between the </h1> and <h1> tags as well.
$string = 'onio<h1>Test1</h1>Bad Content<h1>Test2</h1>09knkj';
 
$pattern = '/<h1>(.*)<\/h1>/';
preg_match_all($pattern, $string, $matches);
 
print_r($matches);

Open in new window

LVL 1
ksecorAsked:
Who is Participating?
 
shobinsunCommented:
Hi,

Use this:


<?
$string = 'onio<h1>Test1</h1>Bad Content<h1>Test2</h1>09knkj';
 
$pattern = '/<h1>[a-zA-Z0-9]*<\/h1>/';
 
preg_match_all($pattern, $string, $matches);
 
print_r($matches);
?>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.