...

View Full Version : simple preg_match question



Nare
11-26-2007, 06:29 PM
Hi, Im trying to write a function and pass an argument that will be used by preg_match. can't get it to work and this seems to be where my problem is:


$subject="54,1,2,3,51,55,4,5,6,5";

$pattern ='/[,]*5[^\d]/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);

//output: Array ( [0] => Array ( [0] => 5, [1] => 13 ) )

$lookfor = "5";
$pattern = "'/[,]*".$lookfor."[^\d]/'";
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);

//output: Array ( )


print_r($pattern);

//output: '/[,]*5[^\d]/'



Pattern is the same in both cases, so what am I missing here?

rpgfan3233
11-26-2007, 06:32 PM
$pattern ='/[,]*5[^\d]/'; //vs
$pattern = "'/[,]*".$lookfor."[^\d]/'";Notice the "' and the '" ? Simple mistake that is easy to overlook. :)

Nare
11-26-2007, 06:34 PM
cheers mate :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum