PHP:仅删除前几个空的
标签
我有一个定制开发的 CMS,用户可以在其中输入一些内容到富文本字段(ckeditor)中。
用户只需从另一个文档复制粘贴数据即可。有时,数据开头有空的
标记。以下是数据示例:
<p></p>
<p></p>
<p></p>
<p>Data data data data</p>
<p>Data data data data</p>
<p>Data data data data</p>
<p>Data data data data</p>
<p></p>
<p></p>
<p>Data data data data</p>
<p>Data data data data</p>
<p></p>
我不想删除所有空
标签,只想删除实际数据之前的标签,即前 3 个
在本例中为 code> 标签。
我怎样才能做到这一点?
编辑:澄清一下,我需要一个 PHP 解决方案。 JavaScript 不行。
有没有一种方法可以将所有
标签收集到一个数组中,然后迭代并删除,直到遇到包含数据的标签?
I have a custom developed CMS where users can enter some content into a rich text field (ckeditor).
Users simply copy-paste data from another document. Sometimes the data has empty <p>
tags at the beginning. Here's a sample of the data:
<p></p>
<p></p>
<p></p>
<p>Data data data data</p>
<p>Data data data data</p>
<p>Data data data data</p>
<p>Data data data data</p>
<p></p>
<p></p>
<p>Data data data data</p>
<p>Data data data data</p>
<p></p>
I don't want to remove all the empty <p>
tags, only the ones before the actual data, the top 3 <p>
tags in this case.
How can I do that?
Edit: To clarify, I need a PHP solution. Javascript won't do.
Is there a way I can gather all <p>
tags in an array, then iterate and delete until I encounter one with data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请不要对不规则字符串使用正则表达式:它 搅动沉睡的神。相反,使用 XPath:
所提供的所有正则表达式解决方案都不好的原因:
)
Please, don't use regular expressions for irregular strings: it stirs the sleeping god. Instead, use XPath:
Reasons why all the regex solutions presented are bad:
<p class="foo"></p>
)<p> </p>
)通常我建议不要使用正则表达式来解析 HTML,但这似乎是无害的:
Normally I would advise against using a regular expression to parse HTML, but this one seems harmless:
使用
Use
您可以在javascript中执行此操作,一旦执行粘贴操作,使用正则表达式去除不需要的标签,
您的代码将类似于,
删除初始空,
编辑:仅
You can do it in javascript, as soon as performs paste operation, strip off unwanted tags using regular expressions,
your code will be like,
Edit: To remove initial empty
only,