2014-01-16 67 views
-2

我正在通过php脚本清除一些html文件,并且我想删除所有不在<tag></tag>之间的所有\n thingsies。如何删除所有换行符在html标签之外

<p>some text</p> 


      <- here are the bunch of \n I want to remove 


<p>some other random 
text with \n at fixed width 
and that's great</p> 

任何想法? 非常感谢。

+1

您的意思是在''标签内,但在其任何子标签之外。 – techfoobar

+0

以外的任何在html代码中。 不好 Lego

+0

为此使用DOM解析器,并删除所有只是空白的子标记。你有没有看过DOMDocument的文档? –

回答

1

这样的事情就足够了吗?

<?php 
$html=<<<SOMECONT 
<p>some text</p> 





<p>some other random 
text with \n at fixed width 
and thats great</p> 
SOMECONT; 

$narr=array_filter(explode(PHP_EOL,$html),'strlen'); 
echo implode('',$narr); 

OUTPUT:

<p>some text</p><p>some other randomtext with 
at fixed widthand thats great</p> 

编辑:另类

可能会更 “脏”,但工程。最后,删除html标签之间的所有\ n有时可以像从原始文件的分解字符串中删除空行一样简单。

$split = explode(PHP_EOL,$data); 
    $data= ""; 
    for($i = 0; $i < count($split); $i++){ 
    $line = $split[$i]; 
    else if(strlen($line) > 0) $data .= $split[$i]."\n"; // filter 
    } 
+0

不会删除html标签之间的换行符吗? – Lego

+2

strlen在这里评估如果长度> 0来删除每个“空行”权利?它应该工作,但不知道它不是。但是我创建了一个肮脏的方法来在PHP_EOL上使用爆炸来做同样的事情,然后使用strlen()进行过滤。感谢 – Lego

+0

@乐高,很高兴你的工作。你可以编辑这个答案,就像你做的一样,所以其他人可以从中受益! –

相关问题