stuckwithcode Posted January 5, 2010 Share Posted January 5, 2010 I am trying to make sure that all line breaks in a piece of text are converted to \r\n. When I use the code below for: a b c $text = str_replace(array("\r\n","\r","\n"), "\r\n", $text); After using mysql_real_escape_string the output shows: a\r\r\n\r\nb\r\r\n\r\nc Where am I going wrong? I expected it to remain at a\r\nb\r\nc If I use $text = str_replace(array("\r\n","\r","\n"), "\r", $text); , it works. Quote Link to comment https://forums.phpfreaks.com/topic/187233-str_replace-help/ Share on other sites More sharing options...
rajivgonsalves Posted January 5, 2010 Share Posted January 5, 2010 I am not sure what could be wrong with the code but you can use preg_replace instead $text = preg_replace('#[\r\n]+#', "\r\n", $text); Quote Link to comment https://forums.phpfreaks.com/topic/187233-str_replace-help/#findComment-988746 Share on other sites More sharing options...
oni-kun Posted January 5, 2010 Share Posted January 5, 2010 Your code is of error, say there already is an \r\n on the page, You'll be replacing it in order, so \r\n = \r\n = \r\r\n\r\n ... The order of the array is important, so you should use something like Rajivgonsalvas' code. Quote Link to comment https://forums.phpfreaks.com/topic/187233-str_replace-help/#findComment-988750 Share on other sites More sharing options...
stuckwithcode Posted January 5, 2010 Author Share Posted January 5, 2010 oni-kun Thanks for the reply but I didn't quite understand your explanation of how I got a\r\r\n\r\nb\r\r\n\r\nc and could someone explain how $text = preg_replace('#[\r\n]+#', "\r\n", $text); works. Quote Link to comment https://forums.phpfreaks.com/topic/187233-str_replace-help/#findComment-988754 Share on other sites More sharing options...
oni-kun Posted January 5, 2010 Share Posted January 5, 2010 oni-kun Thanks for the reply but I didn't quite understand your explanation of how I got a\r\r\n\r\nb\r\r\n\r\nc and could someone explain how $text = preg_replace('#[\r\n]+#', "\r\n", $text); works. You're using an array in your str_replace function. What the array does, is essentially iterates through each one and replaces it. (\r, \n, \r\n) \r \n and \r\n will be replaced. So if on the page there is '\n', it'll become '\r\n'. Then what? There's more elements in the array you have. the '\r' in '\r\n' will become '\r\n\r', and again, '\r\r\n\r'. The array makes you replace the replacement's replacement in other words, you're overwriting your result. Quote Link to comment https://forums.phpfreaks.com/topic/187233-str_replace-help/#findComment-988758 Share on other sites More sharing options...
stuckwithcode Posted January 5, 2010 Author Share Posted January 5, 2010 thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/187233-str_replace-help/#findComment-988790 Share on other sites More sharing options...
salathe Posted January 5, 2010 Share Posted January 5, 2010 To avoid replacing already replaced values, you could instead use the strtr function. Quote Link to comment https://forums.phpfreaks.com/topic/187233-str_replace-help/#findComment-988816 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.