Page 1 of 1

Remove Carriage Returns from textarea Input [SOLVED]

Posted: Sat Mar 19, 2005 9:46 pm
by servo888
[EDIT] Not solved anymore! The text is still not being cleaned. The returns are still left... Damn this really bits. Strangly enough I'm not sure what is going on. The file becomes borked such as

Code: Select all

3/20/05                                          1
John Doe                                        2
Message of some sort goes here       3
  more of the message here
  and some more message.
I need the entire message to be on one line. (line 3)
[/EDIT]

This has been bothering me a lot. I'm building a simple php guestbook, where the input is stored in a file. MySQL would just be overkill, and light sql isn't installed (not my server). So I'm going with a basic file database. Anyways the problem I'm having is that the input provided by the textarea contains (may) returns, which cause havok upon the file database. I need a way to clean the input from everything, but text and spaces.

I've tried

Code: Select all

$CommentClean = preg_replace('&[^a-z\d\s.]+&i', '', $Comment);
and...
$CommentClean2 = strip_tags($text);
But still with out much luck.


Here's a chunck of code...

Code: Select all

//Get the information supplied from the URL. (used if there are more than 10 entries)
//guestbook.php?page=2
$page=$_GET['page'];

$gbdatabase = 'gbdatabase.dat';

/*
// Provides: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");
*/

//Open the database file (creates it if it does not exist) with read/write access
$file = fopen($gbdatabase, 'a+');

if(($Name!="") && ($Email!="") && ($Comment!=""))
{
	//First lets get the date
	$rawdate = getdate();
	//$date is an array, we'll want to pull out the [hour, min] and [month, day, year]
	$date = $date = $rawdate[hours] . ":" . $rawdate[minutes] . " " . $rawdate[mon] . "/" . $rawdate[mday] . "/" . $rawdate[year];

	//$CommentClean = preg_replace('&[^a-z\d\s\r.]+&i', '', $Comment);
	//$CommentClean2 = strip_tags($text);
	$Comment = trim($Comment);
	//Write lines to file (Name / Email, etc stored ontop of each other
	fwrite($file, $date . "\n");
	fwrite($file, $Name . "\n");
	fwrite($file, $Email . "\n");
	fwrite($file, $Comment . "\n");
	fwrite($file, "EndEntry" . "\n"); //just a stop point
	
	//Close file
	fclose($file);
	
	//clear variables
	$Name = "";
	$Email = "";
	$Comment = "";
	$date = "";
}

Posted: Sat Mar 19, 2005 9:48 pm
by servo888
$Comment would be the submited input from the textarea. And for some reason searching these forums I've found a few people asking how to keep these returns... Weird =\ I must be the only one wanting to rid of them.

Posted: Sat Mar 19, 2005 9:51 pm
by feyd
you can edit your post's here...

str_replace() \n with \\n or some other thing you wish to use.

Posted: Sat Mar 19, 2005 10:03 pm
by servo888
feyd wrote:you can edit your post's here...

str_replace() \n with \\n or some other thing you wish to use.
Sorry didn't think anybody would care =\. But thanks it...

Code: Select all

$Comment = str_replace ("\n", " ", "$Comment");
..worked very well! Thanks for the help, you get your good-deed-of-the-day star.


feyd | :grumble:

Posted: Sun Mar 20, 2005 10:22 am
by servo888
Well here's something... I don't think $Comment is an array. echo array_values($Comment); returns Warning: array_values(): The argument should be an array in...

Posted: Sun Mar 20, 2005 10:26 am
by feyd
why would it be an array? It's a string. A textarea submits a string.

Posted: Sun Mar 20, 2005 10:30 am
by servo888
feyd wrote:why would it be an array? It's a string. A textarea submits a string.
I'm thinking C; A string is an array of characters. Ahh this stinks, if it's a string then why wouldn't something like this work? str_replace("\n", " ", "$Comment");

Well it sort of works, fwrite writes it to several lines, BUT reading from the file returns only one (strange but true). So I guess the problem would be I need fwrite to write it on one line.

Posted: Sun Mar 20, 2005 10:37 am
by feyd
you're probably getting line feeds in there too.. try this:

Code: Select all

$Comment = preg_replace('#\r\n|\n\r|\r|\n#', ' ', $Comment);
If you want to keep the formatting they added, you can replace with '<br />' instead of a space...

Posted: Sun Mar 20, 2005 10:46 am
by servo888
feyd wrote:you're probably getting line feeds in there too.. try this:

Code: Select all

$Comment = preg_replace('#\<span style='color:blue' title='ignorance is bliss'>are</span>\n|\n\<span style='color:blue' title='ignorance is bliss'>are</span>|\<span style='color:blue' title='ignorance is bliss'>are</span>|\n#', ' ', $Comment);
If you want to keep the formatting they added, you can replace with '<br />' instead of a space...
Yup it was the line feeds. I looked over the trim man page and the str_replace, and that's when I learned that not only does there a \n exist, but also a \r... I though they were the same :-(

$cleaner = array("\r","\n","\t","\0","\x0B");
$Comment = trim(str_replace($cleaner, " ", "$Comment"));

It seems to be working well right now. So thanks again for the help :-)