...

View Full Version : Need help on preg_match pattern.



ramki067
02-20-2008, 01:17 PM
Hi all,
I've the below code for extracting particular word from each line of the file. The particular word is the first word after a tab in that line and second word is after second tab. When i used the below code, it gives me "Array" word continuously and not my required word from the file. (This Array word is not from the file, its a php given word.) Am i using the right pattern in preg_match as this word will contain a-z,0-9, _ and - in that word. Please help. The words in each line will be as for ex: "test1_WMA_40_v2_128kbps_48kHz_2".



$file1 = "C:\Program Files\Apache Group\Apache2\htdocs\playback_automation_datafile_WMA_48kHz_full.txt";
$lines = file($file1);
foreach($lines as $line_num => $line)
{
//preg_match("/\t([\S| ]+)\t/",$line,$matches[0]);
preg_match("/\t^([a-z0-9_-])\t/",$line,$matches[0]);
echo "$matches[0]<br>";
}


Thanks,
Ramki.

Isaak
02-20-2008, 10:46 PM
Have a try with this code:


$file1 = "C:\Program Files\Apache Group\Apache2\htdocs\playback_automation_datafile_WMA_48kHz_full.txt";
$lines = file($file1);
foreach($lines as $line_num => $line)
{
preg_match("/\t([a-z0-9_\-])\t/i",$line,$matches);
echo "$matches[0]<br>";
}

The reason why you always get an array type is because you used $matches[0] as subpattern which always creates an array with the found values.

oesxyl
02-20-2008, 11:13 PM
try to use instead preg_match, this:



$match = preg_split("/\t/",$line);


best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum