My name is Edward Tanguay. I'm an American software and web developer living and working in Berlin, Germany.
PHP CODE EXAMPLE created on Sunday, May 02, 2010 permalink
Function to trim the preceding and trailing blank lines off an array
This function uses array_slice to remove the blank lines off the beginning and end of an array, but leaves any internal blank lines. Thanks outis.
<?php

$lines[] = '            ';
$lines[] = ' ';
$lines[] = '';
$lines[] = 'first line';
$lines[] = 'second line';
$lines[] = '';
$lines[] = 'fourth line';
$lines[] = '    ';
$lines[] = '';

$lines = trimLines($lines);
foreach($lines as $line) {
    echo '[' . $line . ']<br/>';
}

function trimLines($lines) {
    $end = count($lines);
    for ($start=0; trim($lines[$start]) === ''; ++$start) {
        if ($start == $end) {
            return array();
        }
    }
    for (--$end; trim($lines[$end]) === ''; --$end) {}
    return array_slice($lines, $start, $end-$start+1);
}
?>
need markup?