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 13 of 13
  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    93
    Thanks
    5
    Thanked 0 Times in 0 Posts

    How to Remove Carriage return and line feeds.

    Hi.

    I have form that has a text box. I want insert unformatted text into the database.
    Even if the user gives an carriage return or New line feed in the database it should be stored as on sinle line without any formatting.

    Could someone help me with this?

    Thanks

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,849
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    You could use nl2br() while outputting the textarea contents.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Nov 2005
    Posts
    93
    Thanks
    5
    Thanked 0 Times in 0 Posts

    I dont want to display

    Thanks for the reply,But i am not concerned on displaying the value
    I want to store it in the database without formatting i want to strip off the carriage return and new line feed before it gets stored to the DB

    In the web form i have enter key disabled,But if the user pastes some text from the word document it will contain line feeds and carriage returns.I want to remove them while i store it to the DB.Any idea how to do this?

    Thanks

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    $input trimpreg_replace'/\s+/'' '$input ) ); 
    That will convert the input to one continuous line.

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,849
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Oh .. I see!
    Try something like
    PHP Code:
    $newtext ereg_replace("/\n\r|\r\n/"""$text); 
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by abduraooft View Post
    Oh .. I see!
    Try something like
    PHP Code:
    $newtext ereg_replace("/\n\r|\r\n/"""$text); 
    What? No. What are you trying to do?

    Don't use ereg_* functions... they'll soon be gone. Get with the times dude. ereg is sooo 1998.

    Also, \s captures all whitespace, such as \n, \r, \t, etc.

    And that is bad code; ereg_* doesn't rely on delimiters like preg_* does. Do you know this?

    And let's just pretend that code isn't broken and will work the way you'd like. It will replace all whitespace with nothing. So "This is text" would become "Thisistext". I doubt that is what he wants to achieve.

    I find it hard to believe this is a typical suggestion of yours with 160+ thank yous to your credit.

    Last edited by kbluhm; 04-07-2008 at 04:41 PM.

  • Users who have thanked kbluhm for this post:

    abduraooft (04-07-2008)

  • #7
    New Coder
    Join Date
    Nov 2005
    Posts
    93
    Thanks
    5
    Thanked 0 Times in 0 Posts

    How to Remove Carriage return and line feeds.

    I just want to remove
    New Line Feed(\n)
    Carriage Return(\r)
    spaces at the begin and end of the string.

    Eg:
    If i have
    text1(enter key pressed)
    text2(enter key pressed)
    text3(enter key pressed)
    Blank space
    Blank space

    I want this to be stored in the DB as:
    text1 text2 text3

  • #8
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,849
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Code:
    $input=preg_replace( '/\r\n/', ' ', trim($input) );
    ?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #9
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Yeah...
    Quote Originally Posted by kbluhm View Post
    PHP Code:
    $input trimpreg_replace'/\s+/'' '$input ) ); 
    That will convert the input to one continuous line.

  • #10
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile The Real Solution

    I ran into the same exact problem, and looked all over the web to find a current solution.... then decided to do it myself. This works:

    <?php
    function fixstr($str){
    $str=stripslashes($str); // strip out hash marks to fix apostropies and single quote marks
    $str=strtr ($str,chr(13),'-'); // replace carriage return with dash
    $str=strtr ($str,chr(10),chr(32)); // replace line feed with space
    return $str; // take it back home
    }

    // feed the garbled string to "input" and see the results
    echo '<b>Output:</b> '.fixstr($_POST["input"]).'<br>';

    // append the fixed line of text to a file, adding LF & CR to the end
    $file=fopen("output.txt","a");
    fwrite($file,fixstr($_POST["input"])." \r\n");
    fclose($file);
    ?>

    I guess that makes me an expert. I have only been coding php for a couple of weeks (but programming since '69). I guess that make me an old fart, too!

  • #11
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    1.

    You shouldn't really just stripslashes on everything - you should test for get_magic_quotes_gpc() first:

    PHP Code:
    if (get_magic_quotes_gpc()) $val stripslashes($val); 
    2.

    I love how almost every solution involves regex in some way...

    PHP Code:
    $val str_replace(array("\r\n""\r""\n"), ' '$val); 
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #12
    New Coder
    Join Date
    Sep 2006
    Posts
    21
    Thanks
    1
    Thanked 1 Time in 1 Post
    Quote Originally Posted by ComputerX View Post
    I love how almost every solution involves regex in some way...

    PHP Code:
    $val str_replace(array("\r\n""\r""\n"), ' '$val); 
    Sorry to revive an old thread but ComputerX's code only worked for me if I put the search in single quotes like so:

    PHP Code:
    $val str_replace(array('\r\n''\r''\n'), ' '$val); 

  • #13
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by kbluhm View Post
    PHP Code:
    $input trimpreg_replace'/\s+/'' '$input ) ); 
    That will convert the input to one continuous line.
    I know this is an old thread, but to save every one else doing the same as me and reading all the posts above and still not knowing which methods work.

    THIS ONE DOES. Perfectly.

    If your still monitoring the thread KBLUHM.. Thanks for your help.

    Keep well and happy

    Mike


  •  

    Posting Permissions

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