views:

224

answers:

2

Possible Duplicate:
PHP Regular express to remove <h1> tags (and their content)

I have some HTML that looks like this:

<h2>
Fund Management</h2>
<p>
The majority of property investments are now made via our Funds.</p>

Trying to use a regular expression to strip h2 tags but doesn't work because of the space between the opening and closing h2 tags.

preg_replace('/<h2>(.+?)<\/h2>/', '', $content);

Any ideas on how to make this work?

Also I would ideally like it to replace h1-h6 tags so maybe it needs [1-6] or something?

A: 

This was asked in the last few days > click me

Sres
Thanks, I have now come up with this..`preg_replace('/<h[1-6]+[^>]*?>.*?<\/h[1-6]+>/si', '', $content);`
fire
A: 

The only problem of your regex was the lack of modifiers (the si thingy) but if you you want to extend it to match from <h1> to <h6> tags, you can accomplish this by using a back-reference from first tag:

preg_replace("/<h([1-6]{1})>.*?<\/h\\1>/si", '', $content);

This way you ensure your first tag to match the second.

You can learn more about the modifiers here:

reference.pcre.pattern.modifiers.php

acmatos