Page 1 of 1
newline carriagde return striping problem
Posted: Tue Nov 04, 2008 10:42 am
by lettie_dude
Hi
I am trying to strip newlines and carriage returns from form data input from a text field. I have tried various methods but all seem to be failing.
I am set up on a windows box could this be an issue?
I have tried:
Code: Select all
$string = ereg_replace("/\n\r|\r\n|\n|\r/", " ", $string)
$string = preg_replace("/\t/", " ", $string);
I have also tried nl2br and stripping the <br> tags but this does not work either.
I have tried stripping them before entry into the database and also after retrieval. But nothing seems to be working.
From the above when echoing the string it seems to place everything on one line but also replaces the spaces with the \r\n characters
i.e. some text\r\n\r\nmore text\r\nmore text\r\n\r\n\r\nmore text
I also tried switching the replace string with the replacing space and still same results!
Any assistance appreciated.
Cheers
Re: newline carriagde return striping problem
Posted: Tue Nov 04, 2008 12:05 pm
by Mark Baker
Code: Select all
$stripFrom = array("\n", "\r", "\t");
$stripTo = array(" ", " ", " ");
$string = str_replace($stripFrom, $stripTo, $string)
Re: newline carriagde return striping problem
Posted: Wed Nov 05, 2008 3:50 am
by lettie_dude
Above code from Mark does exactly the same as the code I have used previously and returns below:
Line 1 \r\nLine 2\r\nLine 3\r\n\r\nLine 4 after 2 returns
It seems as though the code is not recognising the \r\n as sepcial characters and is doing something strange by replacing the breaks and returns with the code itself obviously causing the end data to still have the special characters in so when written back to html adds the returns and breaks back in causing the javaScript I am trying to use it in to break. If I check what has been entered in the database in the BLOB it appears with word style paragraph characters and << as returns. Even if I try stripping them from the retrieved data it still does not strip them.
So this is what is entered in the form textarea box:
Line1
Line2
Line3
Line 4 after 2 returns
When the strip code is applied it returns:
Line 1 \r\nLine 2\r\nLine 3\r\n\r\nLine 4 after 2 returns
This is then added to the database and newlines and breaks are re-created as the special characters are interpreted.
I need this to remove any special characters so the end data is one continual string with no carriage returns or breaks.
Re: newline carriagde return striping problem
Posted: Wed Nov 05, 2008 3:54 am
by papa
Does trim() work ?
Re: newline carriagde return striping problem
Posted: Wed Nov 05, 2008 4:41 am
by lettie_dude
trim only removes characters at the beginning and end of a string not from within the string itself.
Re: newline carriagde return striping problem
Posted: Wed Nov 05, 2008 4:47 am
by pcoder
Try to replace the carraige return with new line. May be it works.
Are you using oracle database? I just faced the same issue before , i tried it with
Code: Select all
str_replace( "\r\n","\n", $statements);
It worked perfectly.

Re: newline carriagde return striping problem
Posted: Wed Nov 05, 2008 4:47 am
by VladSun
EDIT: See my post below
Re: newline carriagde return striping problem
Posted: Wed Nov 05, 2008 4:55 am
by pcoder
If you are using oracle, then this problems rises due to the CRLF(carraige return line feed pair) which is common to text files.
The rest of the world treats CRLF as white space but oracle does not.
Try to replace the CRLF with LF, it works.

Re: newline carriagde return striping problem
Posted: Wed Nov 05, 2008 4:56 am
by VladSun
OK, I saved it as MAC format and I was able to see what the problem is:
Code: Select all
$string = "aaaaaaaa\rbbbbbbbb";
$string2 = ereg_replace("/\n\r|\r\n|\n|\r/", " ", $string);
$string3 = preg_replace("/\n\r|\r\n|\n|\r/", " ", $string);
?>
<pre><?php echo $string; ?></pre>
<hr>
<pre><?php echo $string2; ?></pre>
<hr>
<pre><?php echo $string3; ?></pre>
The moral of the story is:
Notes
Tip
preg_replace(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg_replace().
Re: newline carriagde return striping problem
Posted: Wed Nov 05, 2008 5:04 am
by lettie_dude
Hi
Not using Oracle mySql. Replacing the space with /n had no effect.
VladSun code works as is but not when trying to strip textare form input!
Any other ideas?
Re: newline carriagde return striping problem
Posted: Wed Nov 05, 2008 5:05 am
by VladSun
Do you refer to my very last post code?
Re: newline carriagde return striping problem
Posted: Wed Nov 05, 2008 5:38 am
by lettie_dude
Yes just tried with both ereg_replace and preg_replace and both return the same strings
Line 1\r\nLine 2\r\n\r\nLine 3\r\n\r\nLine 4 after 2 returns
Line 1\r\nLine 2\r\n\r\nLine 3\r\n\r\nLine 4 after 2 returns
Again this is happening from form field input in a textarea field.
Re: newline carriagde return striping problem
Posted: Wed Nov 05, 2008 5:55 am
by VladSun
lettie_dude wrote:Again this is happening from form field input in a textarea field.
It doesn't matter ...
Could you post the rest of your code?
Here is my short probe:
Code: Select all
<html>
<body>
<form method="post">
<textarea name="ta"></textarea>
<input type="submit" value="OK">
</form>
<?php
$s1 = empty($_POST['ta']) ? '' : $_POST['ta'];
$s2 = ereg_replace("/\n\r|\r\n|\n|\r/", " ", $s1);
$s3 = preg_replace("/\n\r|\r\n|\n|\r/", " ", $s1);
echo '<pre>'.$s1.'</pre><hr>';
echo '<pre>'.$s2.'</pre><hr>';
echo '<pre>'.$s3.'</pre><hr>';
?>
</body>
</html>
And it works ...
Re: newline carriagde return striping problem
Posted: Wed Nov 05, 2008 6:16 am
by lettie_dude
Hi Vlad
Cheers for your input you got me thinking on the right track. I have now resolved the issue through trial and error. The problem was I was stripping slashes and turning the string into mysql_real_escape_string before doing the stripping which I guess was affecting the type of characters it was returning.