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.

 

 

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

$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

 

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.