View Full Version : How to Remove Carriage return and line feeds.
coolguyraj
04-07-2008, 04:06 PM
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
abduraooft
04-07-2008, 04:11 PM
You could use nl2br() (http://www.php.net/nl2br) while outputting the textarea contents.
coolguyraj
04-07-2008, 04:20 PM
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
kbluhm
04-07-2008, 04:22 PM
$input = trim( preg_replace( '/\s+/', ' ', $input ) );
That will convert the input to one continuous line.
abduraooft
04-07-2008, 04:25 PM
Oh .. I see!
Try something like $newtext = ereg_replace("/\n\r|\r\n/", "", $text);
kbluhm
04-07-2008, 04:28 PM
Oh .. I see!
Try something like $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.
:confused::confused::confused:
coolguyraj
04-07-2008, 05:09 PM
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
abduraooft
04-07-2008, 05:34 PM
$input=preg_replace( '/\r\n/', ' ', trim($input) ); ?
kbluhm
04-07-2008, 06:22 PM
Yeah...
$input = trim( preg_replace( '/\s+/', ' ', $input ) );
That will convert the input to one continuous line.
garybraden
10-25-2009, 06:14 PM
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!
Lamped
10-26-2009, 03:05 AM
1.
You shouldn't really just stripslashes on everything - you should test for get_magic_quotes_gpc() first:
if (get_magic_quotes_gpc()) $val = stripslashes($val);
2.
I love how almost every solution involves regex in some way...
$val = str_replace(array("\r\n", "\r", "\n"), ' ', $val);
FiZiX
03-16-2010, 09:10 PM
I love how almost every solution involves regex in some way...
$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:
$val = str_replace(array('\r\n', '\r', '\n'), ' ', $val);
mikerg
03-14-2012, 09:13 AM
$input = trim( preg_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. :thumbsup:
If your still monitoring the thread KBLUHM.. Thanks for your help.
Keep well and happy
Mike
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.