...

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



ramki067
03-02-2008, 03:29 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>";
}

}



The pattern to be searched is like this:



"test1_WMA_40_v2_128kbps_48kHz_2"


and the file contains these type of names as below:



settop decode -audio 0x1 -mpeg_type ASF -audio_type 0x86 /wma/L2/WMA_V4.0/test1_WMA_40_v2_128kbps_48kHz_2.asf test1_WMA_40_v2_128kbps_48kHz_2 C:\Audio_Regress\recorded_files 90 48000




Thanks,
Ramki.

digitalfiz
03-02-2008, 09:41 AM
I think you need to delimit the underscores when using regexp. you also need a slash in front and back of the pattern.




$pattern = str_replace("_", "\_", $stream_names[$d]);
$pattern = "/".$pattern."/";



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum