Actually, I settled on this function. Problem is that the globals in the lamba are not getting set. I thought perhaps because I had a function in a function. I moved the $track_urls variable outside the function and still get NULL's from my var_dump expressions. Also, where $key is set in the loop, not sure it's "global."
if ( !function_exists('link_tracking_nums') ) {
function link_tracking_nums($html)
{
$track_patterns = array(
'REGX_TRACK_FEDEX'=>REGX_TRACK_FEDEX,
'REGX_TRACK_UPS'=>REGX_TRACK_UPS,
'REGX_TRACK_USPS'=>REGX_TRACK_USPS
);
$track_urls = array(
'REGX_TRACK_FEDEX'=>URL_TRACK_FEDEX,
'REGX_TRACK_UPS'=>URL_TRACK_UPS,
'REGX_TRACK_USPS'=>URL_TRACK_USPS
);
$callback = create_function(
'$matches',
'global $key, $track_urls;'.
'var_dump($key); var_dump($track_urls);'.
'$url = sprintf($track_urls[$key],$matches[0]);'.
'return sprintf("<a href=%s>%s</a>", $url, $matches[0]);'
);
foreach ($track_patterns as $key => $pattern) {
$html = preg_replace_callback(
$pattern, $callback, $html
);
}
return $html;
}
}
Any ideas on getting the $key and $track_urls into my lambda function? I already tried $GLOBALS. Man, I wish I could do this with PHP 5.3 and then I could use a closure with use(). There's probably some refactoring that I could do and go about this differently, but this is possibly the first time that PHP has kind of let me down syntactically. That being said, I know it's not absolutely fair because I'm being way to stubborn on doing this with preg_replace_callback. I have seen examples of people getting globals in a lamda. Seems like I can never make it work. Am I missing something obvious here (besides the fact my code is butt ugly and almost embarrassing?)
Thanks