Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,084
    Thanks
    11
    Thanked 100 Times in 98 Posts

    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)

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Last edited by brothercake; 04-24-2004 at 07:25 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,084
    Thanks
    11
    Thanked 100 Times in 98 Posts
    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)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •