tags:

views:

139

answers:

4

It's supposed to match the text inside any h1, h2, or h3 tags.

preg_match("<[hH][1-3][^>]*>(.*?)<[hH][1-3]>", $text, $matches);
echo $matches[0];

But it never catches any.

+6  A: 

You're missing the forward slashes at the end of the tag and around the regex. Try this:

preg_match("/<[hH][1-3][^>]*>(.*?)<\/[hH][1-3]>/", $text, $matches);
echo $matches[0];

Your regex is looking for <h1>Text<h1> rather than <h1>Text</h1>. Adding the slash will capture the actual HTML.

Eric
Small nit: that's a forward slashNote however that the regexp will match <h1>blah</h2>
kibibu
@kibibu: Thanks, I always get them mixed up.
Eric
Easy way to remember is to think of which way the slash 'falls' if it were a stick figure moving from left to right.
AvatarKava
I don't know why people insist on wrapping their regex's with a slash when it's commonly used in the middle... I prefer a backtick.
Mark
@Mark: Some of us are vimmers and enjoy our / :)
Eric
I always think of a (forward) slash as most convenient for a right-hander to write (moving the arm, not the fingers). Being a member of the sinistral minority, I notice these things.
pavium
It is worth noting that parsing HTML with regular expressions is a Bad Idea™.
Williham Totland
A: 

At a minimum, you're missing delimiters in the regex:

preg_match("/<[hH][1-3][^>]*>(.*?)<[hH][1-3]>/", $text, $matches);
echo $matches[0];
too much php
+5  A: 

You need regex delimiters other than the ones you're effectively winding up using (angle brackets). And the slash on the closing tag. I'm also going to recommend capturing the opening tag and requiring that the closing tag be the same.

preg_match('!<(h[1-3])[^>]*>(.*?)</\1>!i', $text, $matches);
echo $matches[1];
chaos
A: 

I made some improvements to Eric's code. Now it matches < h2 ><h1></h1></ h2 > (not that this should ever happen). There are still some issues, for example try <h1><!--</h1>--></h1>.

preg_match("/<\s*[hH]([1-3])\s*>(.*?)<\/\s*[hH]\1\s*>/", $text, $matches);
echo $matches[0];  // the entire matched string
echo $matches[2];  // the contents between the tags
Imagist