Hi, I was just reading the code of Invisimail and found this comment

// the callback needs to know what filter we're using
// however there's no way to hand off that variable
// so we'll set a global variable

Actually, its possible, if you use a lambda function:

function invisimail($string, $format) {
   static $js = variable_get('invisimail_js_'.$format, 0);
   static $link = variable_get('invisimail_link_'.$format, 0);
   $invisimail_callback = create_function('$matches', 'return $matches[1] . invisimail_ascii_encode($matches[2], '.$js.', '.$link.') . $matches[3];');
   return preg_replace_callback($pattern, $invisimail_callback, $string);
}

This has got the additional advantage that the two variable_get's are called only once, instead of everytime the pattern matches (which was in your old code).
I used zero as default value for variable_get because FALSE evaluates to an empty string. Alternatively you can cast the value to an int in the concattenation.

Comments

Crell’s picture

Status: Active » Closed (won't fix)

create_function() is not a lambda. It's a total hack and is slow. :-) PHP 5.3 supports real lambdas but that's not out yet. In a few years we can consider moving to a real lambda.