Parse error: syntax error, unexpected T_FUNCTION line 10?

The error is likely caused by

return preg_replace_callback($e, function($v) use ($s,$r) { return $r[$v[1]];  },$sql);

Chances are you’re using PHP 5.2 or earlier, which doesn’t support closures. You can find out which version of PHP you’re using phpinfo().

You’ll likely either need to upgrade to PHP 5.3+, or use create_function, or write a static function and pass it as a callback.

Here’s an example of the last option, using a simple class to store the state of $r:

class My_callback {
  public function __construct($s, $r) {
    $this->s = $s; $this->r = $r;
  } 

  function callback($v) { return $this->r[$v[1]]; }
}

function search_replace($s,$r,$sql) {
  $e="/(".implode('|',array_map('preg_quote', $s)).')/';
  $r = array_combine($s,$r);
  $c = new My_callback($s, $r);
  return preg_replace_callback($e, array($c, 'callback'), $sql);
}

Leave a Comment