使用DOMxpath或regex删除


问题描述:

I use DOMxpath to remove html tags that have empty text node but to keep <br/> tags,

$xpath = new DOMXPath($dom);

while(($nodeList = $xpath->query('//*[not(text()) and not(node()) and not(self::br)]')) && $nodeList->length > 0) 
{
    foreach ($nodeList as $node) 
    {
        $node->parentNode->removeChild($node);
    }
}

it works perfectly until I came across another problem,

$content = '<p><br/><br/><br/><br/></p>'; 

How do remove this kind of messy <br/>and<p>? which means I don't want to allow <br/> alone with <p> but I allow <br/> with proper text like this only,

$content = '<p>first break <br/> second break <br/> the last line</p>'; 

Is that possible?

Or is it better with a regular expression?

I tried something like this,

$nodeList = $xpath->query("//p[text()=<br\s*\/?>\s*]");
    foreach($nodeList as $node) 
    {
        $node->parentNode->removeChild($node);
    }

but it return this error,

Warning: DOMXPath::query() [domxpath.query]: Invalid expression in...

我使用DOMxpath删除具有空文本节点的html标记但保留&lt; br /&gt; code>标签, p>

  $ xpath = new DOMXPath($ dom); 
 
while(($ nodeList = $ xpath-&gt; query('// *  [not(text())and not(node())and not(self :: br)]'))&amp;&amp; $ nodeList-&gt; length&gt; 0)
 {
 foreach($ nodeList as  $ node)
 {
 $ node-&gt; parentNode-&gt; removeChild($ node); 
} 
} 
  code>  pre> 
 
 

它完美无缺 我遇到了另一个问题, p>

  $ content ='&lt; p&gt;&lt; br /&gt;&lt; br /&gt;&lt; br /&gt;&lt; br /  &GT;&LT; / p为H.';  
  code>  pre> 
 
 

如何删除这种凌乱的&lt; br /&gt; code>和&lt; p&gt; code>? 这意味着我不想单独使用&lt; br /&gt; code>和&lt; p&gt; code>,但我允许&lt; br /&gt; code >只有这样的正确文字, p>

  $ content ='&lt; p&gt; first break&lt; br /&gt; 第二次休息&lt; br /&gt; 最后一行&lt; / p&gt;';  
  code>  pre> 
 
 

这可能吗? p>

或者使用正则表达式会更好吗? p>

  $ nodeList = $ xpath-&gt; query(“// p [text()=&lt; br \ s * \ /?  &gt; \ s *]“); 
 foreach($ nodeList as $ node)
 {
 $ node-&gt; parentNode-&gt; removeChild($ node); 
} 
  code>   pre> 
 
 

但它返回此错误, p>

 警告:DOMXPath :: query()[domxpath.query]:...中的表达式无效 n  code>  pre> 
  div>

You can select the unwanted p using XPath:

"//p[count(*)=count(br) and br and normalize-space(.)='']"

Note to select empty-text nodes shouldn't you better use (?):

"//*[normalize-space(.)='' and not(self::br)]"

This will select any element (but br) whithout text nodes, nodes like:

<p><b/><i/></p>

or

<p> <br/>   <br/>
</p>

included.

You could get rid of them all by simply checking to see that the only things within a paragraph are spaces and <br /> tags: preg_replace("\<p\>(\s|\<br\s*\/\>)*\<\/p\>","",$content);

Broken down:

\<p\>    # Match for <p>
(        # Beginning of a group
  \s       # Match a space character
  |        # or...
  \<br\s*\/\> # match a <br /> tag, with any number (including 0) spaces between the <br and />
)*       # Match this whole group (spaces or <br /> tags) 0 or more times.
\<\/p\>  # Match for </p>

I will mention, however, that unless your HTML is well-formatted (one-line, no strange spaces or paragraph classes, etc), you should not use regex to parse this. If it is, this regex should work just fine.

I have almost same situation, i use:

$document->loadHTML(str_replace('<br>', urlencode('<br>'), $string_or_file));

And use urlencode() to change it back for display or inserting to database. Its work for me.