newline carriagde return striping problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

newline carriagde return striping problem

Post 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
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: newline carriagde return striping problem

Post by Mark Baker »

Code: Select all

 
$stripFrom = array("\n", "\r", "\t");
$stripTo = array(" ", " ", " ");
$string = str_replace($stripFrom, $stripTo, $string)
 
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Re: newline carriagde return striping problem

Post 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.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: newline carriagde return striping problem

Post by papa »

Does trim() work ?
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Re: newline carriagde return striping problem

Post by lettie_dude »

trim only removes characters at the beginning and end of a string not from within the string itself.
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: newline carriagde return striping problem

Post 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.
:)
Last edited by pcoder on Wed Nov 05, 2008 4:48 am, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: newline carriagde return striping problem

Post by VladSun »

EDIT: See my post below
Last edited by VladSun on Wed Nov 05, 2008 5:08 am, edited 2 times in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: newline carriagde return striping problem

Post 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.
8)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: newline carriagde return striping problem

Post 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().
There are 10 types of people in this world, those who understand binary and those who don't
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Re: newline carriagde return striping problem

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: newline carriagde return striping problem

Post by VladSun »

Do you refer to my very last post code?
There are 10 types of people in this world, those who understand binary and those who don't
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Re: newline carriagde return striping problem

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: newline carriagde return striping problem

Post 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 ...
There are 10 types of people in this world, those who understand binary and those who don't
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Re: newline carriagde return striping problem

Post 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.
Post Reply