Jump to content

Archived

This topic is now archived and is closed to further replies.

CanMan2004

Removing \r\n

Recommended Posts

Hi all

I have some data stored in my sql database and im printing that data onto a php page, the problem i am having is it is printing the line breaks

\r\n

at the end of each line, i've tried many replace scripts and hunted online, but none seem to work, has anyone got a good solution for this?

Thanks in advance

Dave

Share this post


Link to post
Share on other sites
str_replace(array("\n","\r"),"",$string);

Or use [url=http://www.php.net/manual/en/function.nl2br.php]nl2br()[/url] if needed.

Orio.

Share this post


Link to post
Share on other sites
[url=http://www.php.net/manual/en/function.nl2br.php]nl2br()[/url] is clearly where it's at....

Regards
Rich

edit: Ooops, too slow!

Share this post


Link to post
Share on other sites
Hi

Thanks for that, but both those suggestions don't seem to make any difference, it still leaves \r\n at the end of each line

Any other suggestions?

Thanks in advance everyone

Share this post


Link to post
Share on other sites
Hi

Thanks.

Where should that go?

So far I have

print str_replace(array("\n","\r"),"",trim($arow['mytext']));

Which still prints \n\r at the end of each line

Thanks

Share this post


Link to post
Share on other sites
This should be be fine:
[code=php:0]$arow['mytext'] = "hello \n, how \r . \n  you \r\n?";
print str_replace(array("\r\n", "\r", "\n"), '', $arow['mytext']);[/code]

No need for trim.

Share this post


Link to post
Share on other sites
[quote author=wildteen88 link=topic=107211.msg429789#msg429789 date=1157628480]
This should be be fine:
[code=php:0]$arow['mytext'] = "hello \n, how \r . \n  you \r\n?";
print str_replace(array("\r\n", "\r", "\n"), '', $arow['mytext']);[/code]

No need for trim.
[/quote]

Just an old programmer's observation:

The OP didn't mention whether there are no cr/lf characters embedded in the string.

The OP didn't say he wanted the cr/lf characters translated to html.

Given those facts, trim() is a perfectly viable recommendation since it trims the offending characters off the end of the string.

[code=php:0]trim($text);[/code]

If that is indeed all he wants, I think trim() presents a simpler and cleaner solution in the code.




Share this post


Link to post
Share on other sites
Hi

I tried putting

[code]print str_replace(array("\r\n", "\r", "\n"), '', $arow['mytext']);[/code]

But it still prints \r\n

Is this something I wont be able to overcome

Thanks for all the help so far

Regards

Dave

Share this post


Link to post
Share on other sites
[quote author=jsimmons link=topic=107211.msg429826#msg429826 date=1157632577]
[quote author=wildteen88 link=topic=107211.msg429789#msg429789 date=1157628480]
This should be be fine:
[code=php:0]$arow['mytext'] = "hello \n, how \r . \n  you \r\n?";
print str_replace(array("\r\n", "\r", "\n"), '', $arow['mytext']);[/code]

No need for trim.
[/quote]

Just an old programmer's observation:

The OP didn't mention whether there are no cr/lf characters embedded in the string.

The OP didn't say he wanted the cr/lf characters translated to html.

Given those facts, trim() is a perfectly viable recommendation since it trims the offending characters off the end of the string.

[code=php:0]trim($text);[/code]

If that is indeed all he wants, I think trim() presents a simpler and cleaner solution in the code.[/quote]

It doesnt matter whether there is cr/lf characters or not within the string. it'll work when there isnt any, such as when you press retrun in textarea. However your solution will only work if there are cr/lf characters at the beginning or end of a string. It wont remove whitespsace charas from within a string.

Share this post


Link to post
Share on other sites
Is it literally printing "\r\n" or are you seeing a break in the html text? Remember that line breaks in HTML are usually created using <br  />. What is in the actual source of the page?

Share this post


Link to post
Share on other sites
Hi

Thanks for all the help so far, yes it is literally printing

\r\n

no matter what I do, a sample is

This is a test from the db\r\n
With multiple lines being used\r\n
End of test\r\n

If I use any of the replace functions in php, they work fine, but always seem to ignore the \r\n at the end of the lines and still prints them.

Regards

Dave

Share this post


Link to post
Share on other sites
[code]
print str_replace('\r\n',  '', $arow['mytext']);
[/code]

EDIT: made an error

Share this post


Link to post
Share on other sites
Hi everyone

Thanks shoz for that edited version worked great, thanks everyone else for your suggestions and help.

Thanks all so much for your help

Cheers

Dave

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.