techcone Posted January 7, 2009 Share Posted January 7, 2009 I have a webpage that has many linefeeds. I want to replace all lines feeds greater than 2 with only one linefeed. Please can anyone help me with that ?. Thnx in advance. Link to comment https://forums.phpfreaks.com/topic/139906-remove-multiple-lines/ Share on other sites More sharing options...
Brian W Posted January 7, 2009 Share Posted January 7, 2009 please give more details and/or show the code with the LF's Link to comment https://forums.phpfreaks.com/topic/139906-remove-multiple-lines/#findComment-731973 Share on other sites More sharing options...
techcone Posted January 7, 2009 Author Share Posted January 7, 2009 Input : 1 2 3 4 5 6 Output: 1 2 3 4 5 6 I think this will make things clear. Link to comment https://forums.phpfreaks.com/topic/139906-remove-multiple-lines/#findComment-731980 Share on other sites More sharing options...
Brian W Posted January 7, 2009 Share Posted January 7, 2009 (*not tested code) $Lines=explode("\n", $input); $output = ""; foreach($Lines as $Line){ if(trim($Line) > ''){ $output .= $Line."<br>"; } } echo $output; hope that helps Link to comment https://forums.phpfreaks.com/topic/139906-remove-multiple-lines/#findComment-731995 Share on other sites More sharing options...
techcone Posted January 7, 2009 Author Share Posted January 7, 2009 Well it is removing all the lines. I want the output as above. Thanx for your effort. Link to comment https://forums.phpfreaks.com/topic/139906-remove-multiple-lines/#findComment-732001 Share on other sites More sharing options...
Brian W Posted January 7, 2009 Share Posted January 7, 2009 $Lines=explode("\n", $input); $output = ""; $last = ""; foreach($Lines as $Line){ if($last != "<br>"){ $output .= trim($Line)."<br>"; $last = trim($Line); } } echo $output; yeah, my bad... try that Link to comment https://forums.phpfreaks.com/topic/139906-remove-multiple-lines/#findComment-732007 Share on other sites More sharing options...
Brian W Posted January 7, 2009 Share Posted January 7, 2009 hmm, was pondering and this may also work... $output = str_replace("\n", "\n " $input); //add spaces after all \n's $output = str_replace(" ", " " $output); //replace double spacing with single $output = str_replace("\n \n ", "\n ", $output); //replace all occurrences of two \n's with single Link to comment https://forums.phpfreaks.com/topic/139906-remove-multiple-lines/#findComment-732057 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.