Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Delete from file

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Delete from file

Postby GeOrGe » Fri Jul 19, 2002 12:06 am


Hello everyone.
How can I delete from a file some entries?
e.g.
This is the conf.php file
Code: Select all
//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 :?:
GeOrGe
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Jul 18, 2002 11:56 pm

Postby GeOrGe » Fri Jul 19, 2002 1:04 am

Code: Select all
   $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
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Jul 18, 2002 11:56 pm

Postby WiZARD » Fri Jul 19, 2002 1:23 am

GeOrGe wrote:
Code: Select all
   $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
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.