|
Posted by "M. Sokolewicz" on 07/12/05 12:22
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 =
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
Navigation:
[Reply to this message]
|