CanMan2004 Posted September 7, 2006 Share Posted September 7, 2006 Hi allI 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\nat 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 advanceDave Quote Link to comment https://forums.phpfreaks.com/topic/19996-removing-rn/ Share on other sites More sharing options...
Orio Posted September 7, 2006 Share Posted September 7, 2006 str_replace(array("\n","\r"),"",$string);Or use [url=http://www.php.net/manual/en/function.nl2br.php]nl2br()[/url] if needed.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/19996-removing-rn/#findComment-87634 Share on other sites More sharing options...
HuggieBear Posted September 7, 2006 Share Posted September 7, 2006 [url=http://www.php.net/manual/en/function.nl2br.php]nl2br()[/url] is clearly where it's at....RegardsRichedit: Ooops, too slow! Quote Link to comment https://forums.phpfreaks.com/topic/19996-removing-rn/#findComment-87636 Share on other sites More sharing options...
CanMan2004 Posted September 7, 2006 Author Share Posted September 7, 2006 HiThanks for that, but both those suggestions don't seem to make any difference, it still leaves \r\n at the end of each lineAny other suggestions?Thanks in advance everyone Quote Link to comment https://forums.phpfreaks.com/topic/19996-removing-rn/#findComment-87645 Share on other sites More sharing options...
onlyican Posted September 7, 2006 Share Posted September 7, 2006 at the end of the line, u want trim()removes \n \r \t Quote Link to comment https://forums.phpfreaks.com/topic/19996-removing-rn/#findComment-87647 Share on other sites More sharing options...
CanMan2004 Posted September 7, 2006 Author Share Posted September 7, 2006 HiThanks.Where should that go?So far I haveprint str_replace(array("\n","\r"),"",trim($arow['mytext']));Which still prints \n\r at the end of each lineThanks Quote Link to comment https://forums.phpfreaks.com/topic/19996-removing-rn/#findComment-87650 Share on other sites More sharing options...
wildteen88 Posted September 7, 2006 Share Posted September 7, 2006 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 Link to comment https://forums.phpfreaks.com/topic/19996-removing-rn/#findComment-87652 Share on other sites More sharing options...
jsimmons Posted September 7, 2006 Share Posted September 7, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/19996-removing-rn/#findComment-87685 Share on other sites More sharing options...
CanMan2004 Posted September 7, 2006 Author Share Posted September 7, 2006 HiI tried putting[code]print str_replace(array("\r\n", "\r", "\n"), '', $arow['mytext']);[/code]But it still prints \r\nIs this something I wont be able to overcomeThanks for all the help so farRegardsDave Quote Link to comment https://forums.phpfreaks.com/topic/19996-removing-rn/#findComment-87690 Share on other sites More sharing options...
wildteen88 Posted September 7, 2006 Share Posted September 7, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/19996-removing-rn/#findComment-87693 Share on other sites More sharing options...
shoz Posted September 7, 2006 Share Posted September 7, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/19996-removing-rn/#findComment-87714 Share on other sites More sharing options...
CanMan2004 Posted September 7, 2006 Author Share Posted September 7, 2006 HiThanks for all the help so far, yes it is literally printing\r\nno matter what I do, a sample isThis is a test from the db\r\nWith multiple lines being used\r\nEnd of test\r\nIf 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.RegardsDave Quote Link to comment https://forums.phpfreaks.com/topic/19996-removing-rn/#findComment-87725 Share on other sites More sharing options...
shoz Posted September 7, 2006 Share Posted September 7, 2006 [code]print str_replace('\r\n', '', $arow['mytext']);[/code]EDIT: made an error Quote Link to comment https://forums.phpfreaks.com/topic/19996-removing-rn/#findComment-87730 Share on other sites More sharing options...
HuggieBear Posted September 7, 2006 Share Posted September 7, 2006 Here's an idea, can we see your code and an example record from the DB?Rich Quote Link to comment https://forums.phpfreaks.com/topic/19996-removing-rn/#findComment-87731 Share on other sites More sharing options...
CanMan2004 Posted September 7, 2006 Author Share Posted September 7, 2006 Hi everyoneThanks shoz for that edited version worked great, thanks everyone else for your suggestions and help.Thanks all so much for your helpCheersDave Quote Link to comment https://forums.phpfreaks.com/topic/19996-removing-rn/#findComment-87734 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.