thecommonthread Posted March 31, 2011 Share Posted March 31, 2011 So I have this textarea in an HTML form that I need to convert the contents of from having multiple line breaks into only one line break for each. Example: I want to convert the following: I went to the store. I had a good time. I ran into a friend. Into this: I went to the store. I had a good time. I ran into a friend. Any help would be super appreciated. Link to comment https://forums.phpfreaks.com/topic/232351-converting-multiple-lines-breaks-into-singles/ Share on other sites More sharing options...
btherl Posted April 1, 2011 Share Posted April 1, 2011 You may be able to do it like this: $text = preg_replace("|\n+|", "\n", $text); It may not work if the line character is not just "\n". If it doesn't work, try "\r\n" in place of both of the "\n". Another approach is to use explode() or preg_split() to put the lines in an array, then remove empty lines, then join() them together again. Link to comment https://forums.phpfreaks.com/topic/232351-converting-multiple-lines-breaks-into-singles/#findComment-1195313 Share on other sites More sharing options...
thecommonthread Posted April 1, 2011 Author Share Posted April 1, 2011 I tried your first method and couldn't make it work unfortunately. If I used the explode() method, how would I remove empty lines in the array? Maybe I'm not understanding this method completely. Link to comment https://forums.phpfreaks.com/topic/232351-converting-multiple-lines-breaks-into-singles/#findComment-1195324 Share on other sites More sharing options...
cssfreakie Posted April 1, 2011 Share Posted April 1, 2011 I tried your first method and couldn't make it work unfortunately. If I used the explode() method, how would I remove empty lines in the array? Maybe I'm not understanding this method completely. can you show how you tried it, with code Link to comment https://forums.phpfreaks.com/topic/232351-converting-multiple-lines-breaks-into-singles/#findComment-1195331 Share on other sites More sharing options...
thecommonthread Posted April 8, 2011 Author Share Posted April 8, 2011 I used the code above: $text = preg_replace("|\n+|", "\n", $text); I couldn't get it to work, and I tried using \r or \r\n as well. Link to comment https://forums.phpfreaks.com/topic/232351-converting-multiple-lines-breaks-into-singles/#findComment-1198991 Share on other sites More sharing options...
dcro2 Posted April 8, 2011 Share Posted April 8, 2011 Try: $text = preg_replace("|[\r\n]+|", "\n", $text); Link to comment https://forums.phpfreaks.com/topic/232351-converting-multiple-lines-breaks-into-singles/#findComment-1198995 Share on other sites More sharing options...
thecommonthread Posted April 8, 2011 Author Share Posted April 8, 2011 Thank you so so so much, that worked. PHEW! :-\ Link to comment https://forums.phpfreaks.com/topic/232351-converting-multiple-lines-breaks-into-singles/#findComment-1199018 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.