Jump to content

[SOLVED] remove lines in text file via php script


dfwcomputer

Recommended Posts

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.

 

 

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;
    }
  }
}

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

$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') ) );

;D

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.