...

View Full Version : Problems finding a pattern in file. Please help.



ramki067
02-29-2008, 10:39 AM
Hi,
I need to search a pattern in each line of a file and if that pattern found then it should extract that complete line from the file and write it to another file. I've written the code but it not working!Please help. $Data is getting the data. $pattern is present in that file which i checked it out manually.Is my searching correct?



$handle = fopen($Filename, 'r');
$File1 = "Automation_streams.txt";
$Handle1 = fopen($File1, 'w');
$d=0;

while(!feof($handle))
{
$Data = fgets($handle,1024);
$pattern = $stream_names[$d];
if(preg_match($pattern,$Data)!=0)
{
echo "Matched data is $Data<br>";
fwrite($Handle1,$Data);
$d++;
print "<p>";
}

}


Thanks,
Ramki.

Fumigator
02-29-2008, 05:52 PM
echo $pattern and echo $Data and show us those values.

Inigoesdr
02-29-2008, 05:57 PM
Without looking at anything else, the array you want to use for the matched data should be the third argument in the preg_match() (http://php.net/preg_match) function, and you're trying to use $Data inside of the if switch, which should be the subject. If you want more specific help you need to post your pattern, and an example you're trying to match.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum