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.
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.
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.
At a minimum, you're missing delimiters in the regex:
preg_match("/<[hH][1-3][^>]*>(.*?)<[hH][1-3]>/", $text, $matches);
echo $matches[0];
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];
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