Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-24-2004, 05:16 PM   PM User | #1
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
How can I serialise a whole file?

I have a large text file full of line breaks, and I want to be able to import the entire file as though it were a single string, strip it of all line-breaks and then add different ones back in.

But how do I read the file in? I tried using this kind of thing:
Code:
$fcontents = file("file.txt");

while (list ($line_num, $line) = each ($fcontents))
{
But I can't remove the line breaks, as though they're "hard" breaks that aren't actually part of each line.

What can I do about this?
__________________
"Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark
brothercake is offline   Reply With Quote
Old 04-24-2004, 05:31 PM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
Post

PHP Code:
<?
$str
='';
$fcontents file"file.txt" );
foreach( 
$fcontents as $line ){
 
$str .= trim$line ) ;
}
?>
does the above work ?
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 04-24-2004, 05:33 PM   PM User | #3
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
what are "hard" breaks ? "<br />" or "\n" ? Can you post a piece of the file?
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 04-24-2004, 06:14 PM   PM User | #4
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
The source file is actually a .js archive - I didn't think that was relevant. When I try ereg_replace on each line using a whitespace regex [\s] or [ \t\n\r\f] - it doesn't work - doesn't remove the line break.

But trim() works
__________________
"Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

Last edited by brothercake; 04-24-2004 at 07:25 PM..
brothercake is offline   Reply With Quote
Old 04-24-2004, 07:45 PM   PM User | #5
mordred
Senior Coder


 
Join Date: Jun 2002
Location: frankfurt, german banana republic
Posts: 1,848
Thanks: 0
Thanked 0 Times in 0 Posts
mordred is an unknown quantity at this point
Quote:
Originally Posted by brothercake
When I try ereg_replace on each line using a whitespace regex [\s] or [ \t\n\r\f] - it doesn't work - doesn't remove the line break.
Hint:
ereg_replace() operates with POSIX conforming character groups, if you want to use Perl-like regular expression characters like \s, \d etc. you should use preg_replace(), which is also faster and generally more powerful.
__________________
De gustibus non est disputandum.
mordred is offline   Reply With Quote
Old 04-24-2004, 10:50 PM   PM User | #6
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
Quote:
Originally Posted by mordred
Hint:
ereg_replace() operates with POSIX conforming character groups, if you want to use Perl-like regular expression characters like \s, \d etc. you should use preg_replace(), which is also faster and generally more powerful.
Oh really .. now that's good to know But even so - "\n" should have worked shouldn't it?

It's academic now, but I don't understand why it didn't.
__________________
"Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark
brothercake is offline   Reply With Quote
Old 04-25-2004, 04:20 AM   PM User | #7
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
Quote:
But even so - "\n" should have worked shouldn't it?
or a "\r\n" for win32 (which is why I use trim as it gets em all )
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:14 PM.


Advertisement
Log in to turn off these ads.