开发者

preg replace h2 tags with spaces [duplicate]

开发者 https://www.devze.com 2023-01-01 04:27 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: PHP Regular express to remove <h1> tags (and their content)
This question already has answers here: Closed 12 years ago.

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 t开发者_StackOverflowags so maybe it needs [1-6] or something?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消