使用DOM或正则表达式删除

 

使用DOM或正则表达式删除<p>&nbsp; </ p>

问题描述:

How can I remove this type p tag <p>&nbsp;</p> using DOM or regex?

I want to remove multiple p like this too,

<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

如何删除此类型的p标签&lt; p&gt;&amp; nbsp;&lt; / p&gt; code>使用DOM还是正则表达式? p>

我想删除多个这样的p, p>

 &lt; p&gt;&amp;  nbsp;&lt; / p&gt; 
&lt; p&gt;&amp; nbsp;&lt; / p&gt; 
&lt; p&gt;&amp; nbsp;&lt; / p&gt; 
  code>  pre> 
  div  >

If you want to remove a string that is exactly, always, '<p>&nbsp;</p>', the simplest and fastest solution is probably to use str_replace() :

$new_string = str_replace('<p>&nbsp;</p>', '', $old_string);

I don't think it's necessary to use DOM for such a simple case -- and a regex is not necessary here.


Of course, if you need to replace something more complex, that is not always exactly the same string... well, it'll be time for DOM manipulations ;-)

preg_replace("|<p>&nbsp;</p>|", "", "<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>");

In case you would like to do that with xpath (your example is just demanding str_replace however), you can query the &nbsp entity as a string (Demo):

$html = '<body><p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Not empty :)</p>
</body>';

$dom = new DomDocument();
$dom->loadhtml($html);
$xpath = new DomXPath($dom);
$col = $xpath->query("//p[text()=\"\xC2\xA0\"]"); # &nbsp;
foreach($col as $e) {
    $e->parentNode->removeChild($e);
}
echo $dom->saveXML($dom->getElementsByTagName('body')->item(0));

Hope this is helpful if you need to query &nbsp; with xpath.

See as well: Using XPATH to search text containing