我正在通过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>
任何想法? 非常感谢。
我正在通过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>
任何想法? 非常感谢。
这样的事情就足够了吗?
<?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
}
您的意思是在'
'标签内,但在其任何子标签之外。 – techfoobar以外的任何在html代码中。 好 不好好 –
Lego
为此使用DOM解析器,并删除所有只是空白的子标记。你有没有看过DOMDocument的文档? –