karimali831 Posted November 20, 2011 Share Posted November 20, 2011 Hello, When I post content to database and query from database \r\n\ is suppose to be new line indent so if I posted this: test test hello I will get: test\r\ntest\r\n\r\nhello\r\nhello How to fix this? Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/251511-how-to-get-rid-of-rn/ Share on other sites More sharing options...
Fadion Posted November 21, 2011 Share Posted November 21, 2011 From the PHP Manual (but slightly modified): <?php // Order of replacement $str = "Line 1\nLine 2\rLine 3\r\nLine 4\n"; $order = array("\r\n", "\n", "\r"); $replace = ''; // Processes \r\n's first so they aren't converted twice. $newstr = str_replace($order, $replace, $str); ?> That is removing them completely from the string. Instead, if you need to display them as new lines, you can use the nl2br() function, that will convert linebreaks to an HTML break (<br />). <?php $text = "test\r\ntest\r\n\r\nhello\r\nhello"; echo nl2br($text); ?> Your question wasn't quite clear as to remove linebreaks or show them correctly, so I gave both versions. Quote Link to comment https://forums.phpfreaks.com/topic/251511-how-to-get-rid-of-rn/#findComment-1289861 Share on other sites More sharing options...
tomfmason Posted November 21, 2011 Share Posted November 21, 2011 From the PHP Manual (but slightly modified): <?php // Order of replacement $str = "Line 1\nLine 2\rLine 3\r\nLine 4\n"; $order = array("\r\n", "\n", "\r"); $replace = ''; // Processes \r\n's first so they aren't converted twice. $newstr = str_replace($order, $replace, $str); ?> That is removing them completely from the string. Instead, if you need to display them as new lines, you can use the nl2br() function, that will convert linebreaks to an HTML break (<br />). <?php $text = "test\r\ntest\r\n\r\nhello\r\nhello"; echo nl2br($text); ?> Your question wasn't quite clear as to remove linebreaks or show them correctly, so I gave both versions. You could also just use trim to remove the new line characters. Although I think nl2br is what he is actually looking for. Quote Link to comment https://forums.phpfreaks.com/topic/251511-how-to-get-rid-of-rn/#findComment-1289864 Share on other sites More sharing options...
Fadion Posted November 21, 2011 Share Posted November 21, 2011 @tomfmason, doesn't trim() just remove them in the beginning or end of a string? The OP's string had linebreaks in the middle... Quote Link to comment https://forums.phpfreaks.com/topic/251511-how-to-get-rid-of-rn/#findComment-1289865 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.