Click here to get back home

Remove trailing blank lines at the end of a file

 HomeNewsGroups | Search

comp.lang.php - PHP programming language discussions 

get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Remove trailing blank lines at the end of a file Luk 03-30-2009
Posted by Luk on March 30, 2009, 9:51 am
Please log in for more thread options
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
Please log in for more thread options
Luk wrote:

show/hide quoted text

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
Please log in for more thread options
On 30 Mar, 16:05, Iv=E1n S=E1nchez Ortega <ivansanchez-...@rroba-
show/hide quoted text
PHP
show/hide quoted text
1, 2.54

I used trim. But without results. Other hints?

thanks

Luk

Posted by Iván Sánchez on March 30, 2009, 3:56 pm
Please log in for more thread options
Luk wrote:
show/hide quoted text

$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
Please log in for more thread options
On 30 Mar, 21:56, Iv=E1n S=E1nchez Ortega <ivansanchez-...@rroba-
show/hide quoted text

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);
show/hide quoted text
//stuff
}

Luk

Similar ThreadsPosted
Using File() with blank lines June 4, 2005, 2:21 pm
remove comment lines from .txt file December 9, 2008, 12:05 pm
file download three blank lines at start July 3, 2005, 4:21 pm
remove all lines starting with b March 26, 2006, 9:23 pm
blank line at beginning of downloaded file March 15, 2005, 6:15 pm
Remove duplicate entries from TXT file ? January 16, 2005, 6:31 pm
Skip lines in txt-file October 11, 2004, 11:54 am
Read first 10 lines from a file August 19, 2005, 12:50 pm
edit lines in txt file April 24, 2007, 8:54 pm
Newbie: How to loop through lines in a file? December 15, 2004, 3:25 am

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

Friends:

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap
Privacy Policy