Delete from file

A

Anonymous

Guest

Hello everyone.
How can I delete from a file some entries?
e.g.
This is the conf.php file
Code:
//start_entry1
$title .= '4~~~3';
$writer .= '4~~~';
$counting .= 'count3 ~~~';
//end_entry1
//start_entry2
$title .= '51~~~4';
$writer .= '51~~~';
$counting .= 'count4 ~~~';
//end_entry2

//New_entry
I want to delete from //start_entry1 to //end_entry1.
Any ideas :?:
 
Code:
   $filename = "./conf.php";
   $fd = fopen ($filename, "r");
   $DELETE = "//start_entry1";
   echo"$DELETE";
   $m_line=3;$tmp_line=0;$line=0;
   $m_filename = "./conf2.php";
   $m_fd = fopen($m_filename, "w");
      while (!feof ($fd)){
         $buffer = fgets($fd, 4096);
         $pos = strpos ($buffer, $DELETE);
            if ($pos === false and $line==0){
               fputs ($m_fd,$buffer);
            }
            else{
               if ($tmp_line < $m_line){
                  $tmp_line = $tmp_line + 1;
                  $line = 1;
               }
               else{
                  $line = 0;
               }
            }
      }
   fclose($fd);
   fclose($m_fd);
   copy("./conf2.php","./conf.php");

No need of //end_entry1
 
GeOrGe said:
Code:
   $filename = "./conf.php";
   $fd = fopen ($filename, "r");
   $DELETE = "//start_entry1";
   echo"$DELETE";
   $m_line=3;$tmp_line=0;$line=0;
   $m_filename = "./conf2.php";
   $m_fd = fopen($m_filename, "w");
      while (!feof ($fd)){
         $buffer = fgets($fd, 4096);
         $pos = strpos ($buffer, $DELETE);
            if ($pos === false and $line==0){
               fputs ($m_fd,$buffer);
            }
            else{
               if ($tmp_line < $m_line){
                  $tmp_line = $tmp_line + 1;
                  $line = 1;
               }
               else{
                  $line = 0;
               }
            }
      }
   fclose($fd);
   fclose($m_fd);
   copy("./conf2.php","./conf.php");

No need of //end_entry1

Hi!
I think what you need create array from your file and delete id from array it's faster variant than you propose, but if file too big you have a problem with speed your maschine
 
Back
Top