Click here to get back home

Remove trailing blank lines at the end of a file

 HomeNewsGroups | Search

get this group's latest topics as an RSS feed  comp.lang.php - PHP programming language discussions

Subject Author Date
Remove trailing blank lines at the end of a file Luk 03-30-2009
If you were  Registered and logged in, you could reply and use other advanced thread options
Posted by Luk on March 30, 2009, 9:51 am
Hello to all,
I'm far from being an expert of PHP, but I'm fighting with a light
procedure to remove trailing blank lines at the end of a file I've in
input from a user.

I'm using this code, but the lines containing a space and a \n at the
end, still remain :-(

$search  = array(" ","\r\n","\r","\t","\n");
$replace = "";
$userfile = str_replace($search, $replace, $userfile);
$userfile=preg_replace("/^\n*$/","",$userfile);

//$userfile still has trailing blank lines at the end of file. Why?

many thanks!

Luk

Posted by Iván Sánchez on March 30, 2009, 10:05 am
Luk wrote:



Because you're not using trim().

--
----------------------------------
Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

Proudly running Debian Linux with 2.6.26-1-amd64 kernel, KDE 3.5.10, and PHP
5.2.6-3 generating this signature.
Uptime: 16:05:28 up 5 days, 19:06,  4 users,  load average: 3.52, 2.81, 2.54


Posted by Luk on March 30, 2009, 3:20 pm
On 30 Mar, 16:05, Iv=E1n S=E1nchez Ortega <ivansanchez-...@rroba-


I used trim. But without results. Other hints?

thanks


Luk

Posted by Iván Sánchez on March 30, 2009, 3:56 pm
Luk wrote:


$userfile holds the *contents* and not the *name* of the file, right?
Right??

You are writing the trimmed stuff back to the file, right?

--
----------------------------------
Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

Existen tres tipos de personas: las que saben contar y las que no.

Posted by Luk on March 31, 2009, 4:36 am
On 30 Mar, 21:56, Iv=E1n S=E1nchez Ortega <ivansanchez-...@rroba-


Yes, should be. This is the code used:

if (move_uploaded_file($_FILES['uploaded_file']['tmp_name'],
$userfile)) {
    echo 'OK';
}

then after the checks about the spaces and other chars, there's the
foreach cicle that works:

$lines =3D file($userfile);

    //stuff
}

Luk

If you were  Registered and logged in, you could reply and use other advanced thread options

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Driving a better car - Fuelzilla.com

Cabling site for homeowners and pros alike - Cabling-Design.com

1-Script XML SitemapXML Sitemap
Privacy Policy