0

i have a text file contents as below.

++ myhost1
menu = sanjay
title = sanjay host
host =192.168.0.247


++ gateway1
menu = Gateway1
title=gateway
host=192.168.0.227


i need a php code to remove these lines.

++ gateway1
menu = Gateway1
title=gateway
host=192.168.0.227


Can any one help me plzz ? thanks in advance.

3
Contributors
5
Replies
20
Views
6 Years
Discussion Span
Last Post by DILO3D
0

Does your file just contain one such line:

++gateway1

or many...

If it is just the one you could do a string search for ++gateway1 then remove that and the three line afterwards.

The idea is simple. Read the file into memory, perform the removal, delete original file and then create new file with lines removed.

0

Read line by line from the file using fgets() function.
Store the text u needed on a variable.(Use if statement for that );
After finishing the read operation close the file and again open it in write mode(w).
Write the contents that we are already stored on the variable to the file.

0

Thankyou for your reply. I used this way. But it is not worked.Can u plz help me?

while(!feof($file))
  {

$data=fgets($file)."<br />";

$data1="++ gateway1";

if($data==$data1)
{
echo "found";
}

}

Read line by line from the file using fgets() function.
Store the text u needed on a variable.(Use if statement for that );
After finishing the read operation close the file and again open it in write mode(w).
Write the contents that we are already stored on the variable to the file.

0

Try the following code.
I used ex.txt to store your data file.

<?php
$file1 = fopen("ex.txt", "r") or exit("Unable to openfile!");
//Output a line of the file until the end is reached
$t="";
while(!feof($file1))
  {
$k= fgets($file1);
$pattern = "++ gateway1";

if ( (preg_match("/gateway1/", $k)) || (preg_match("/menu = Gateway1/", $k)) || (preg_match("/title=gateway/", $k)) || (preg_match("/192.168.0.227/", $k))   )
 {

 }
else
{
 $t=$t.$k;
}

  }
 
fclose($file1);

$file = fopen("ex.txt", "w") or exit("Unable to open file!");

fwrite($file,$t);
fclose($file);

?>

Edited by divyakrishnan: n/a

0

I m very thankful for your reply. It works fine.But if i mentioned like (preg_match("/title=gateway/", $k) , it is deleting the lines which starts with that word.
Example:

it delets "title=gateway2"

what is the solution for it?
-------------------------------------------------------------------
other problem
---------------------------------------------------------------
if i delete a line,i have to delete the empty line followed by that Deleted line?
how can i do it?


plzz help me.

Try the following code.
I used ex.txt to store your data file.

<?php
$file1 = fopen("ex.txt", "r") or exit("Unable to openfile!");
//Output a line of the file until the end is reached
$t="";
while(!feof($file1))
  {
$k= fgets($file1);
$pattern = "++ gateway1";

if ( (preg_match("/gateway1/", $k)) || (preg_match("/menu = Gateway1/", $k)) || (preg_match("/title=gateway/", $k)) || (preg_match("/192.168.0.227/", $k))   )
 {

 }
else
{
 $t=$t.$k;
}

  }
 
fclose($file1);

$file = fopen("ex.txt", "w") or exit("Unable to open file!");

fwrite($file,$t);
fclose($file);

?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.