Re: how to strip empty lines out of a txt using preg_replace()
| From: | Clancy | Date: | Sat, 05 Sep 2009 02:45:02 +0000 |
| Subject: | Re: how to strip empty lines out of a txt using preg_replace() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-297720@lists.php.net to get a copy of this message | ||
On Fri, 4 Sep 2009 14:58:55 +0200, ralph_deffke@yahoo.de ("Ralph Deffke") wrote:
>Hi all, I'm a bit under stress, maybe somebody knows the regex on a snap.
>using PHP_EOL would be great.
Here is a function which works for files pasted from browser screens:
function clean_up($dirty_file)
{
$contents = file_get_contents ($dirty_file);
if ($contents)
{
$c_array = explode ("\n", $contents);
$n = count($c_array);
$i = 0; $j = 0; while ($i < $n)
{
$line = trim($c_array[$i]);
if ($line != '')
{
$d_array[$j++] = $c_array[$i];
}
++$i;
}
$clean = implode ("\n",$d_array);
$data_file = file_put_contents($data_file, $clean);
}
}
The virtue of this is you don't have to know, or care, what characters are on the blank
lines.