stuckwithcode Posted January 5, 2010 Share Posted January 5, 2010 Hello I have this code below that converts all types of line breaks to \r\n. $text = preg_replace('#[\r\n]+#', "\n", $text); But if there are 5 line breaks it groups them to 1. I want to keep the number of line breaks the same, just make them all the same format. If I remove the + it seems to double the actual number of line breaks. Can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/187268-line-breaks/ Share on other sites More sharing options...
premiso Posted January 5, 2010 Share Posted January 5, 2010 Just use str_replace as this is rather simple: $text = str_replace("\r\n", "\n", $text); You generally only want to use the preg functions when you need advance replacement / finding of contents inside a string. Since you do not, str_replace will due and would be preferred. Quote Link to comment https://forums.phpfreaks.com/topic/187268-line-breaks/#findComment-988899 Share on other sites More sharing options...
stuckwithcode Posted January 5, 2010 Author Share Posted January 5, 2010 But would $text = str_replace("\r\n", "\n", $text); Repace \r\n and \r and \n or just \r\n Quote Link to comment https://forums.phpfreaks.com/topic/187268-line-breaks/#findComment-988900 Share on other sites More sharing options...
premiso Posted January 5, 2010 Share Posted January 5, 2010 Just \r\n. Please do not create multiple topics on the same subject. One will suffice, for future reference. Try this for your preg code: $text = preg_replace("#[\r\n]+#", "\n", $text); As the \r\n characters are taken literally inside of single quotes, but inside of double quotes they are the actual linebreak characters. EDIT: If you want to be able to see it by reading it you can use this to verify that it works: $text = preg_replace("#[\r\n]+#", " REPLACED ", $text); That way, any \n, \r etc characters that get replaced are visible by the REPLACED word. Quote Link to comment https://forums.phpfreaks.com/topic/187268-line-breaks/#findComment-988902 Share on other sites More sharing options...
stuckwithcode Posted January 5, 2010 Author Share Posted January 5, 2010 If i take the text a b and put it into the function the one line break get doubled. e.g a\r\nb goes to a\r\n\r\nb what am I doing wrong. I just want to convert all types of line breaks to \r\n and keep the number of line breaks. Quote Link to comment https://forums.phpfreaks.com/topic/187268-line-breaks/#findComment-988908 Share on other sites More sharing options...
premiso Posted January 5, 2010 Share Posted January 5, 2010 I do not know how you are getting double line breaks. Using this code it: <?php $text = "this is just \r test \r\n text inside \n of text \n\r more text"; $text2 = preg_replace("#[\r\n]+#", "REPLACED", $text); echo $text2 . "<br /><pre>"; $text = preg_replace("#[\r\n]+#", "\n", $text); echo $text; ?> Displays this: this is just REPLACED test REPLACED text inside REPLACED of text REPLACED more text this is just test text inside of text more text Like it should. Quote Link to comment https://forums.phpfreaks.com/topic/187268-line-breaks/#findComment-988912 Share on other sites More sharing options...
stuckwithcode Posted January 5, 2010 Author Share Posted January 5, 2010 but what if there are two line breaks as below: $text = "this is just \r test \r\n\r\n text inside \n of text \n\r more text"; the echo should be: this is just test text inside of text more text but instead it is: this is just test text inside of text more text How can I keep the multiple line breaks? Quote Link to comment https://forums.phpfreaks.com/topic/187268-line-breaks/#findComment-988916 Share on other sites More sharing options...
premiso Posted January 5, 2010 Share Posted January 5, 2010 <?php $text = "this is just \r test \r\n text inside \n of text \n\r more text \r\n\r\n after the window \n\r\n\r ok"; $text = preg_replace("#[\r\n]{1,2}#", "\n", $text); echo $text; Should give: this is just test text inside of text more text after the window ok Quote Link to comment https://forums.phpfreaks.com/topic/187268-line-breaks/#findComment-988921 Share on other sites More sharing options...
stuckwithcode Posted January 5, 2010 Author Share Posted January 5, 2010 Thats exactly what I wanted. Can you please tell me how "#[\r\n]{1,2}#" achieves what I wanted, what does the 1 and 2 do. Will this code work for multiple lines in a row or is there a limit. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/187268-line-breaks/#findComment-988929 Share on other sites More sharing options...
premiso Posted January 5, 2010 Share Posted January 5, 2010 It will match the following: \n \r \r\n \n\r The 1,2 means it will only match 1 or both of those items and no more, the + would match any number of characters \r\n\n\r as long as they were sequential and replace them once. Quote Link to comment https://forums.phpfreaks.com/topic/187268-line-breaks/#findComment-988934 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/187268-line-breaks/#findComment-988936 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.