Need help in preg_match pattern matching.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ramki067
Forum Newbie
Posts: 4
Joined: Wed Feb 20, 2008 2:25 am

Need help in preg_match pattern matching.

Post by ramki067 »

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"

Code: Select all

 
$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.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Need help in preg_match pattern matching.

Post by Stryks »

You wont be able to echo the results as it is an array, hence PHP just outputting 'array'. Use ...

Code: Select all

print_r($matches[0]);
... to display the contents of an array for test purposes.

Also, you might want to change $matches[0] to $matches in the regex line, and access the results array with $matches[0]. If your desire is to create an array across multiple lines, then append to the search results array afterwards. So ...

Code: Select all

preg_match("/\t^([a-z0-9_-])\t/",$line,$matches);
$results[$line_id] = $matches[0];
// or even
$results[] = $matches[0]; // if you dont need to know what line the result was found on.
Test that, and see what your regex is returning.

Hope this helps. :)
ramki067
Forum Newbie
Posts: 4
Joined: Wed Feb 20, 2008 2:25 am

Re: Need help in preg_match pattern matching.

Post by ramki067 »

Stryks wrote:You wont be able to echo the results as it is an array, hence PHP just outputting 'array'. Use ...

Code: Select all

print_r($matches[0]);
... to display the contents of an array for test purposes.

Also, you might want to change $matches[0] to $matches in the regex line, and access the results array with $matches[0]. If your desire is to create an array across multiple lines, then append to the search results array afterwards. So ...

Code: Select all

preg_match("/\t^([a-z0-9_-])\t/",$line,$matches);
$results[$line_id] = $matches[0];
// or even
$results[] = $matches[0]; // if you dont need to know what line the result was found on.
Test that, and see what your regex is returning.

Hope this helps. :)
Hey thanks for the reply.

After using the above its giving me the following error:

Code: Select all

Notice: Undefined offset: 0 in C:\Program Files\Apache Group\Apache2\htdocs\selected_stream.php on line 127
My code is below:

Code: Select all

 
$file1 = "C:\Program Files\Apache Group\Apache2\htdocs\playback_automation_datafile_WMA_48kHz_full.txt";
$lines = file($file1);
$i=0;
foreach($lines as $line_num => $line)
{
  preg_match("/\t^([a-z0-9_-])\t/",$line,$matches);
  $results[] = $matches[0]; 
  print_r($results[$i]);
  $i++;
}
 
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Need help in preg_match pattern matching.

Post by Stryks »

I might be getting mixed up with preg_match_all().

Easiest just to have a look at the output you are getting from preg_match(). Insert the following after line 7 of your code ..

Code: Select all

print_r($matches);
exit();


In your browser, get a 'view source' and post the output of the print_r();
Post Reply