preg 用空格替换 h2 标签
可能的重复:
PHP 正则表达式删除标签(及其内容)
我有一些看起来像这样的 HTML:
<h2>
Fund Management</h2>
<p>
The majority of property investments are now made via our Funds.</p>
尝试使用正则表达式来剥离 h2 标签,但由于开始和结束 h2 标签之间有空格而不起作用。
preg_replace('/<h2>(.+?)<\/h2>/', '', $content);
关于如何实现这项工作有什么想法吗?
另外,我理想地希望它替换 h1-h6 标签,所以也许它需要 [1-6] 或其他东西?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正则表达式的唯一问题是缺少修饰符(
si
之类的东西),但如果您想将其扩展以从匹配到
< ;h6>
标签,您可以通过使用第一个标签的反向引用来完成此操作:这样您可以确保第一个标签与第二个标签匹配。
您可以在此处了解有关修饰符的更多信息:
reference.pcre.pattern。修饰符.php
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:This way you ensure your first tag to match the second.
You can learn more about the modifiers here:
reference.pcre.pattern.modifiers.php