Re: how to delete lines in a txt file using php.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



hi sokolewicz,
 
i get this error, wehn i try to run the file
Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM in C:\apachefriends\xampp\htdocs\fmf\IV-test-files\200501t\dirread2.php on line 21


"M. Sokolewicz" <tularis@xxxxxxx> 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> 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):
$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) {
foreach($deleteLines as $delete) {
if(0 !== strpos($data, $delete)) {
// string does not start with one of the ToBeDeleted parts
// this (combines strpos() !== false || strpos() > 0)
//
// so, let's write it back to the file
fwrite($stream, $data, strlen($data));
}
}
}

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

I'm assuming here you want to only delete lines STARTING with the part 
given in the array.

- tul

		
---------------------------------
Yahoo! Messenger NEW - crystal clear PC to PCcalling worldwide with voicemail

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux