【发布时间】:2010-05-02 10:44:01
【问题描述】:
这个脚本:
<?php
$lines[] = '';
$lines[] = 'first line ';
$lines[] = 'second line ';
$lines[] = '';
$lines[] = 'fourth line';
$lines[] = '';
$lines[] = '';
$lineCount = 1;
foreach($lines as $line) {
echo $lineCount . ': [' . trim($line) . ']<br/>';
$lineCount++;
}
?>
产生这个输出:
1: []
2: [first line]
3: [second line]
4: []
5: [fourth line]
6: []
7: []
什么是更改上述脚本的最快、最有效的方法,以便它同时删除 preceding 和 trailing 空白条目,但 不是内部 strong> 空白条目,以便输出:
1: [first line]
2: [second line]
3: []
4: [fourth line]
我可以使用 foreach 循环,但我想有一种方法可以使用 array_filter 或类似的方法,效率更高。
【问题讨论】: