
Originally Posted by
bluewalrus
Do you know how I can send a variable defined elsewhere with the preg_replace_callback?
PHP Code:
$css = preg_replace_callback("/<link.*?href=\"(.*?)\".*/i", "filtersource($domain)", $source);
function filtersource($location, $domain) {
}
This fails
Found it on php.net, "To access a local variable within a callback, use currying (delayed argument binding). For example"
PHP Code:
<?php
function curry($func, $arity) {
return create_function('', "
\$args = func_get_args();
if(count(\$args) >= $arity)
return call_user_func_array('$func', \$args);
\$args = var_export(\$args, 1);
return create_function('','
\$a = func_get_args();
\$z = ' . \$args . ';
\$a = array_merge(\$z,\$a);
return call_user_func_array(\'$func\', \$a);
');
");
}
function on_match($transformation, $matches)
{
return $transformation[strtolower($matches[1])];
}
$transform = array('a' => 'Well,', 'd'=>'whatever', 'b'=>' ');
$callback = curry(on_match, 2);
echo preg_replace_callback('/([a-z])/i', $callback($transform), 'Abcd');
echo "\n";
?>
Didn't find this when I was at this point though so you can also use the global as djr33 suggests.
Bookmarks