【问题标题】:remove empty tags on top and bottom of html string using php querypath使用php querypath删除html字符串顶部和底部的空标签
【发布时间】:2012-11-06 01:18:06
【问题描述】:

我正在使用 php Querypath,我正在尝试从 html 字符串中删除所有出现在 html 顶部和底部的空标记。例如:

      <div>&nbsp;</div>
      <div>&nbsp;</div>
      <div>I want remove empty div tag on top and bottom</div>
      <div>&nbsp;</div>
      <div>other content</div>
      <div>&nbsp;</div>
      <div>&nbsp;</div>

使用 php 查询路径我想更改为:

      <div>I want remove empty div tag on top and bottom</div>
      <div>&nbsp;</div>
      <div>other content</div>

我该怎么办?

【问题讨论】:

标签: php html-parsing querypath


【解决方案1】:

使用替换字符串将 div 替换为空白。

str_replace(find,replace,from);

因此,您可以在 find 中使用 &lt;div&gt;&amp;nbsp;&lt;/div&gt; 并将其替换为代码中的 ""

【讨论】:

  • 谢谢。我正在尝试这个,但 str_replace 也替换了我想要保留它的内容中间的
     
    。我只想替换那些在顶部和底部而不是在中间的。你有什么想法吗?
  • 所以,您只想替换&lt;div&gt; &lt;/div&gt; 部分,而不是像&amp;nbsp; 这样的中间部分,对吗?
  • 不完全是。我想删除整个标签
     
    但只有那些出现在 html 的顶部和底部。如果您看到上面的示例 5 "
     
    " 存在于 html 字符串中。我想删除第一个,第二个“
     
    ”,然后是第四个和第五个。它必须保持第三,因为它位于“
    我想删除顶部和底部的空 div 标签
    ”和“
    其他内容
    之间
  • @Eritrea 又读了一遍整个问题...他想删除整个空的 div 部分...但不是在填充的 div 之间
猜你喜欢
  • 1970-01-01
  • 2017-12-08
  • 2016-08-25
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
相关资源
最近更新 更多