Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php querypath删除html字符串顶部和底部的空标记_Php_Html Parsing_Querypath - Fatal编程技术网

使用php querypath删除html字符串顶部和底部的空标记

使用php querypath删除html字符串顶部和底部的空标记,php,html-parsing,querypath,Php,Html Parsing,Querypath,我正在使用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>

我正在使用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>

我想删除顶部和底部的空div标签
其他内容
使用php querypath,我希望更改为:

      <div>I want remove empty div tag on top and bottom</div>
      <div>&nbsp;</div>
      <div>other content</div>
我想删除顶部和底部的空div标记
其他内容

如何操作?

使用replace string将div替换为空白

str_replace(find,replace,from);

因此,您可以使用“查找”中的
,并将其替换为代码中的

谢谢。我正在尝试这一点,但用str_replace也可以替换我想要保存的内容中间的内容。我只想更换那些在顶部和底部,而不是在中间。你有一个想法吗?所以,你只想替换<代码> <代码>部分,但是,不是中间的,比如<代码> <代码>,对吗?不完全正确。我想删除整个标记,但只有那些在html的顶部和底部存在。如果您看到上面的示例,则html字符串中有5个“”。我要先删除第一个,第二个“”,然后删除第四个和第五个。它必须保持在第三位,因为它位于“我想删除顶部和底部的空div标记”和“其他内容”之间@厄立特里亚再次阅读整个问题。。。他想删除整个空div部分。。。但在填充的DIVSO之间,你不想在中间保留一个代码> <代码>。然后您可以尝试使用PHP DOM,