dfwcomputer Posted February 5, 2008 Share Posted February 5, 2008 Just wondering if this is possible, i want to open a text file or even a html file and remove extra lines e.g. <table width="200" border="0" cellspacing="0" cellpadding="0"> <tr> <td >$title</td> </tr> </table> once the above code had been run through it would look like this. <table width="200" border="0" cellspacing="0" cellpadding="0"> <tr> <td >$title</td> </tr> </table> Is this possible. Link to comment https://forums.phpfreaks.com/topic/89505-solved-remove-lines-in-text-file-via-php-script/ Share on other sites More sharing options...
laffin Posted February 5, 2008 Share Posted February 5, 2008 depending on the size of file, if ya hunting blank lines inluding spaces & tabs there are many ways to do this Link to comment https://forums.phpfreaks.com/topic/89505-solved-remove-lines-in-text-file-via-php-script/#findComment-458457 Share on other sites More sharing options...
Aureole Posted February 5, 2008 Share Posted February 5, 2008 This might work... str_replace( "\n\n", "\n", $t ); ... where $t is the content of the file. I'm assuming that the space between the table and the tr tag (etc.) is created by 2 \n... though I could be wrong. Link to comment https://forums.phpfreaks.com/topic/89505-solved-remove-lines-in-text-file-via-php-script/#findComment-458458 Share on other sites More sharing options...
dfwcomputer Posted February 5, 2008 Author Share Posted February 5, 2008 actually i just wanted it to scan and remove complete lines with nothing on them.It might not be possible just save a hell of a lot of time lol Link to comment https://forums.phpfreaks.com/topic/89505-solved-remove-lines-in-text-file-via-php-script/#findComment-458615 Share on other sites More sharing options...
laffin Posted February 5, 2008 Share Posted February 5, 2008 Aureole gave ya the simplest answer. Link to comment https://forums.phpfreaks.com/topic/89505-solved-remove-lines-in-text-file-via-php-script/#findComment-458719 Share on other sites More sharing options...
dfwcomputer Posted February 5, 2008 Author Share Posted February 5, 2008 How could i put that into a file that would load test.txt for example. Link to comment https://forums.phpfreaks.com/topic/89505-solved-remove-lines-in-text-file-via-php-script/#findComment-459267 Share on other sites More sharing options...
dfwcomputer Posted February 5, 2008 Author Share Posted February 5, 2008 Something like this maybe? $files = "test.txt"; foreach ($files as $file) { $lines = file($file); foreach ($lines as $line) { $line = str_replace( "\n\n", "\n", $line); $tmp = explode(' ',$line); if ($tmp[$pos] == $search) { return $line; } } } Link to comment https://forums.phpfreaks.com/topic/89505-solved-remove-lines-in-text-file-via-php-script/#findComment-459269 Share on other sites More sharing options...
kenrbnsn Posted February 6, 2008 Share Posted February 6, 2008 Here's my take on the solution: <?php $inp = file('test.txt'); $out = array_map('mytrim',$inp); $out1 = array_filter($out,'remove_blank'); $fp = fopen('test1.txt','w'); fwrite($fp,implode("\r\n",$out1)."\r\n"); fclose($fp); function mytrim($str) { return(str_replace(array("\r\n","\n","\r"),'',$str)); } function remove_blank($str) { return(!(strlen($str) == 0)); } ?> Ken Link to comment https://forums.phpfreaks.com/topic/89505-solved-remove-lines-in-text-file-via-php-script/#findComment-459304 Share on other sites More sharing options...
resago Posted February 6, 2008 Share Posted February 6, 2008 $f=file_get_contents('text.html'); $f = str_replace( "/\n\n+/", "\n", $f); file_put_contents('text.html',$f); not sure if you need the /s modifier. or file_put_contents('text.html',str_replace( "/\n\n+/", "\n", file_get_contents('text.html') ) ); Link to comment https://forums.phpfreaks.com/topic/89505-solved-remove-lines-in-text-file-via-php-script/#findComment-459330 Share on other sites More sharing options...
dfwcomputer Posted February 6, 2008 Author Share Posted February 6, 2008 Here's my take on the solution: <?php $inp = file('test.txt'); $out = array_map('mytrim',$inp); $out1 = array_filter($out,'remove_blank'); $fp = fopen('test1.txt','w'); fwrite($fp,implode("\r\n",$out1)."\r\n"); fclose($fp); function mytrim($str) { return(str_replace(array("\r\n","\n","\r"),'',$str)); } function remove_blank($str) { return(!(strlen($str) == 0)); } ?> Ken Works perfectly m8 thanks very much, much appreciated. Link to comment https://forums.phpfreaks.com/topic/89505-solved-remove-lines-in-text-file-via-php-script/#findComment-459378 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.