i attempted following code:
$for_callback=create_function('$match','return $globals[\'replacements\'][$match[1]];'); $result = preg_replace_callback( $regex, '$for_callback', $string);
the variable $globals['replacements']
generated dynamically before function called.
i error message like
warning: preg_replace_callback() [function.preg-replace-callback]: requires argument 2, '$for_callback', valid callback in...
created functions , callbacks both new me. growing out of code given me nickb @ my question preg_replace turned preg_replace_callback
.
what i'm trying wrap code in answer function , i'm running errors scope avoiding re-defining function. (upgrading php 5.3+ remote posibility option me @ moment.)
how work?
first, variables must not enclosed single quotes, not replaced real value.
and second, should use anonymous functions (i.e. closures) instead easier. use them in example:
$for_callback = function($match) { return $globals['replacements'][$match[1]]; }; $result = preg_replace_callback( $regex, $for_callback, $string);
edit: closures became available in php 5.3. if still using php < 5.3 should (really update or) use following:
$for_callback=create_function('$match','return $globals[\'replacements\'][$match[1]];'); $result = preg_replace_callback( $regex, $for_callback, $string);
Comments
Post a Comment