Jump to content

Removing \r\n


CanMan2004

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
Link to comment
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.




Link to comment
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.
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.