Jump to content


Photo

Removing \r\n


  • Please log in to reply
14 replies to this topic

#1 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 07 September 2006 - 10:35 AM

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

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 07 September 2006 - 10:53 AM

str_replace(array("\n","\r"),"",$string);

Or use nl2br() if needed.

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 07 September 2006 - 10:55 AM

nl2br() is clearly where it's at....

Regards
Rich

edit: Ooops, too slow!
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#4 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 07 September 2006 - 11:19 AM

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

#5 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 07 September 2006 - 11:23 AM

at the end of the line, u want trim()
removes \n \r \t
Tell me the problem, I will try tell you the solution

#6 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 07 September 2006 - 11:26 AM

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

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 September 2006 - 11:28 AM

This should be be fine:
$arow['mytext'] = "hello \n, how \r . \n  you \r\n?";
print str_replace(array("\r\n", "\r", "\n"), '', $arow['mytext']);

No need for trim.

#8 jsimmons

jsimmons
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 07 September 2006 - 12:36 PM

This should be be fine:

$arow['mytext'] = "hello \n, how \r . \n  you \r\n?";
print str_replace(array("\r\n", "\r", "\n"), '', $arow['mytext']);

No need for trim.


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.

trim($text);

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






#9 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 07 September 2006 - 12:40 PM

Hi

I tried putting

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

But it still prints \r\n

Is this something I wont be able to overcome

Thanks for all the help so far

Regards

Dave

#10 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 September 2006 - 12:42 PM

This should be be fine:

$arow['mytext'] = "hello \n, how \r . \n  you \r\n?";
print str_replace(array("\r\n", "\r", "\n"), '', $arow['mytext']);

No need for trim.


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.

trim($text);

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


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.

#11 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 07 September 2006 - 01:04 PM

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?

#12 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 07 September 2006 - 01:32 PM

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

#13 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 07 September 2006 - 01:42 PM

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

EDIT: made an error

#14 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 07 September 2006 - 01:43 PM

Here's an idea, can we see your code and an example record from the DB?

Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#15 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 07 September 2006 - 01:46 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users