...

View Full Version : How can I serialise a whole file?



brothercake
04-24-2004, 05:16 PM
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:


$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?

firepages
04-24-2004, 05:31 PM
<?
$str='';
$fcontents = file( "file.txt" );
foreach( $fcontents as $line ){
$str .= trim( $line ) ;
}
?>


does the above work ?

raf
04-24-2004, 05:33 PM
what are "hard" breaks ? "<br />" or "\n" ? Can you post a piece of the file?

brothercake
04-24-2004, 06:14 PM
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 :thumbsup:

mordred
04-24-2004, 07:45 PM
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.

brothercake
04-24-2004, 10:50 PM
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.

firepages
04-25-2004, 04:20 AM
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 )



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum