Re: how to strip empty lines out of a txt using preg_replace()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 4 Sep 2009 14:58:55 +0200, ralph_deffke@xxxxxxxx ("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.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux