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

Posted on 2009-04-24
Last Modified: 2013-12-13
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);


Open in new window

Question by:ksecor
    1 Comment
    LVL 14

    Accepted Solution


    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);

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now