fagnonk Posted August 10, 2009 Share Posted August 10, 2009 So I am parsing a text file with fopen and I need a way to automatically replace multiple line breaks with single line breaks. Currently, my code simply removes all line breaks but I need a way to allow sinlge line breaks. Is this something I could do with regex? If so, how would I go about it? <?php $v1 = $_GET['fileId']; $v2 = $_GET['title']; if (isset($_POST[$v1])) { $str = $_POST[$v1]; $xml = "".$str.""; $fp = fopen('cms/'.$v1.'.xml', 'w'); $replace = str_replace(chr(10), " ", $xml); //remove carriage returns $replace2 = str_replace(chr(13), "", $replace); //remove carriage returns fwrite($fp, $replace2); fclose($fp); } ?> Link to comment https://forums.phpfreaks.com/topic/169557-removing-multiple-line-breaks/ Share on other sites More sharing options...
scvinodkumar Posted August 10, 2009 Share Posted August 10, 2009 Please move this topic to Regex. preg_replace("/( )+/","<br>",$xml); or preg_replace("/( )+/","</p><p>",$xml); Link to comment https://forums.phpfreaks.com/topic/169557-removing-multiple-line-breaks/#findComment-894616 Share on other sites More sharing options...
fagnonk Posted August 10, 2009 Author Share Posted August 10, 2009 Cool, that worked well. I can't use html expressions so I changed the code a little to look like: $replace = preg_replace("/(\r\n)+/","\n\n",$xml); Here comes the difficult part, can you think of a way that I can allow up to two line breaks? Link to comment https://forums.phpfreaks.com/topic/169557-removing-multiple-line-breaks/#findComment-894622 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.