• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 414
  • Last Modified:

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

0
ksecor
Asked:
ksecor
1 Solution
 
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now