M. Sokolewicz wrote:
babu wrote:

Hi i have a txt file like [SUBSTRAT]
TYP=25x25_10
SUBSTRATNAME=S112
PIXEL=5
OPERATOR=Zi
KOMMENTAR=dunkel
INTENSITAET=1000.000000
[MESSUNG]
DATUM=03.01.2005
UHRZEIT=11:22
MESSUNG=SWEEP_UI
i want to delete lines which start with '[' and 'TYP'. i have multiple such files.i have used strstr method. i have written code for it, but it results in deletion of complete data from files. Can anyone check where the logic goes wrong.
here is my file.
<?
$del_lines=array("[","TYP","P_MPP","I_MPP","V_MPP","T=","FIT","INTENSITAET");
$dir=getcwd();
$handle = opendir($dir);
while (false !== ($file = readdir($handle))) {
if ((preg_match('/^2005\d{4}_\d{4}\.txt$/', $file))||(preg_match('/^2005\d{4}_\d{4}\_01_01\.txt$/',$file))){
     $fc=file($file);
     $f=fopen($file,"w");
     foreach($fc as $line){
      $bool=TRUE;
     for($i=0;$i<sizeof($del_lines);$i++){
          if(!strstr($line,$del_lines[$i])){
            $bool=FALSE;}
             break;
           //fputs($f,$line);
        }
    //if($bool){fputs($f,$line);}
   }
   if($bool){fputs($f,$line);}
fclose($f);
    } }
closedir($handle);
?>
--------------------------------- Yahoo! Messenger NEW - crystal clear PC to PCcalling worldwide with voicemail


wouldn't something like this work (faster):
<?php
$deleteLines =
> [...]
>

Actually, I see I've made a small mistake in that script, here's an updated one:
<?php
$deleteLines = array("[","TYP","P_MPP","I_MPP","V_MPP","T=","FIT","INTENSITAET");

if($handle = opendir('.')) {
    while(($file = readdir($handle)) !== false) {
        // init vars
        $contents = '';

if (!(preg_match('/^2005\d{4}_\d{4}\.txt$/', $file) || preg_match('/^2005\d{4}_\d{4}\_01_01\.txt$/',$file))){
            // the filename has been rejected; move on to the next one
            continue;
        }

        // right, we need to open the file now so we can delete the lines
        $contents    = file($file);
        $stream        = fopen($file, 'w');

        // loop trough the file, examining each line
        foreach($contents as $line=>data) {
                        $clearLine = false;
            foreach($deleteLines as $delete) {
                if(0 === strpos($data, $delete)) {
                    // string starts with one of the ToBeDeleted parts
                    $clearLine = true;
                }
            }
            if(!$clearLine) {
                fwrite($stream, $data, strlen($data));
            }
        }

        fclose($stream);
    }
}
closedir($handle);
?>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to