PDA

View Full Version : if preg_match problem...


poncho
01-13-2006, 10:19 PM
Hey guys,

I have a problem and hope someone can point me in the right direction :-)
I've put this test function together to find keys that pass the regex and append them to the $count array, but it seems that I'm not filtering the keys properly.


$Rexex = '/^(eventDates)\s?([0-9]+)$/';

function countDates ($form_result) {
$count = array();
while(list($key, $val) = each($form_result)) {
if(preg_match($Regex, $key)) {
$count[$key] = $val;
}
}
return $count;
}

print_r(countDates($_GET));


Calling this with "?eventDates1=123&eventDates2=234&startTime1=345" should be giving me back the following, but it's just giving an empty array:


Array
(
[eventDates1] => 123
[eventDates2] => 234
)


Obviously, the if preg_match isn't returning true, but I have no idea why not. The string 'eventDates1' passes the regex no problem and so do the other keys in the $_GET array except for startTime1.

Any help would be appreciated.

Cheers;
Poncho

Kid Charming
01-13-2006, 10:27 PM
I'm assuming the $Rexex instead of $Regex is a typo. Either way, though, that variable is not available inside your function. You need to either set up the var inside the function, pass it to the function with the array you're checking, or make the variable global. Personally, if I only needed the regex for that function, I'd create it inside the function, and if I needed it for more than one, I'd pass it, but that's just 'coz I don't like global variables.

poncho
01-13-2006, 10:34 PM
Thanks for the reply,

Yes, that was a typo, I'll pass the variable and give that a go, cheers.

poncho
01-13-2006, 10:36 PM
Brilliant!

Such a simple thing can really mess things up!

Thanks again, cheers
Poncho