horseatingweeds Posted August 3, 2007 Share Posted August 3, 2007 I'm taking some text from a textarea input and putting it into an html email. I'd like to translate any line breaks into new paragraphs. Anyone know a good way to do this? I'm thinking str_replace() but there is the problem of different OP making different line break characters. Quote Link to comment https://forums.phpfreaks.com/topic/63136-how-to-replace-rn-n-or-r-with/ Share on other sites More sharing options...
seikan Posted August 3, 2007 Share Posted August 3, 2007 This is what I done in my email script: str_replace("\n\r", "<br><br>", $content); //Replace \n\r str_replace("\n", "<br><br>", $content); //Replace single \n str_replace("\r", "<br><br>", $content); //Replace single \r Quote Link to comment https://forums.phpfreaks.com/topic/63136-how-to-replace-rn-n-or-r-with/#findComment-314621 Share on other sites More sharing options...
horseatingweeds Posted August 3, 2007 Author Share Posted August 3, 2007 That seems simple enough. If fixes one of my worries by replacing \n\r first. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/63136-how-to-replace-rn-n-or-r-with/#findComment-314626 Share on other sites More sharing options...
shen Posted August 3, 2007 Share Posted August 3, 2007 Use preg_replace, it is more efficient than str_replace preg_replace("\r\n", "<br><br>", $content); Quote Link to comment https://forums.phpfreaks.com/topic/63136-how-to-replace-rn-n-or-r-with/#findComment-314668 Share on other sites More sharing options...
bibby Posted August 3, 2007 Share Posted August 3, 2007 str_replace() can also take an array. $crap = array("\r\n", "\n\r", "\n","\r"); $new = str_replace($crap, "<br><br>", $content); //Replace everything smartly Quote Link to comment https://forums.phpfreaks.com/topic/63136-how-to-replace-rn-n-or-r-with/#findComment-314669 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.