SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Thread: Trim problem

  1. #1
    SitePoint Enthusiast that_tim_fella's Avatar
    Join Date
    Feb 2003
    Location
    England
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trim problem

    Hi,

    I'm trying to take the data entered into a form element and write it into a text file. First I want to strip out any whitespace and especially carriage returns and newlines.

    I think Trim() should handle this very easily, but I don't seem to be getting the required results in my text file. Here's my code:

    PHP Code:
    // $form_var passed from form input
    $form_var trim($form_var);
    $fp fopen("edit.txt""w");
    fwrite($fp$form_var);
    fclose($fp); 
    So if I write say,

    This

    Sucks

    in the form, I get exactly the same output in the text file, whereas I want it to say - This Sucks

    Why is this? Thanks.

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this maybe:
    PHP Code:
     
    $form_var 
    str_replace("\n"" "$form_var);
    $form_var str_replace("\n\r"" "$form_var);
    $form_var str_replace("\r"" "$form_var); 
    I don't know if that is all the "options" though

    -Helge

  3. #3
    SitePoint Enthusiast that_tim_fella's Avatar
    Join Date
    Feb 2003
    Location
    England
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That worked nicely thanks Helge.

    I'm still wondering why Trim() doesn't do it though. From the PHP reference I'm looking at it should strip " ", "\t", "\n", "\r", "\o", "\x0B" which is basically (as I understand it) the same thing str_replace() is doing.

  4. #4
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    trim() only strips around the string.
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  5. #5
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    trim() only removes from the beginning and the end of a string. It does not remove what's "in the middle".
    I think.

    -Helge

  6. #6
    SitePoint Enthusiast that_tim_fella's Avatar
    Join Date
    Feb 2003
    Location
    England
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah-ha, yes, that makes perfect sense now, thanks.

  7. #7
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Helge
    Something like this maybe:
    PHP Code:
     
    $form_var 
    str_replace("\n"" "$form_var);
    $form_var str_replace("\n\r"" "$form_var);
    $form_var str_replace("\r"" "$form_var); 
    I don't know if that is all the "options" though

    -Helge

    it's
    \r\n not \n\r
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  8. #8
    SitePoint Enthusiast wogboy's Avatar
    Join Date
    Jun 2002
    Location
    Melbourne
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And you can even do it in one operation:
    PHP Code:
    <?php

    $form_var 
    str_replace(array("\n""\r""\r\n"), ' '$form_var);

    ?>

  9. #9
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    one regular expression is probably faster than 3 str_replace()s:

    PHP Code:
    <?php

    $form_var 
    preg_replace('/\r\n?|\n/'' '$form_var);

    ?>
    and also, if you did use str_replace(), you'd only need to do it twice -- for \r and for \n, not \r\n together because the other 2 would take care of that.
    Last edited by DR_LaRRY_PEpPeR; Mar 3, 2003 at 20:05.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  10. #10
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    true, but you'd want to do \r\n first because that represents one Windows Linebreak, and doing it for \r and \n seperately would put 2 spaces for every windows linebreak.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  11. #11
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh, forgot about the spaces. in that case, use my preg_replace().


Bookmarks

Posting Permissions

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