Parse error: syntax error, unexpected T_FUNCTION line 10?

谁说胖子不能爱 提交于 2019-11-27 05:24:39

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);
}

For anyone getting this error on PHP 5.3+ and especially with a wordpress theme, I would recommend having a look at the formatting of the actual files on the server.

When I encountered this error and viewed the PHP files throwing the error on the server, they had no line breaks and were effectively minified to one line.

For some reason, Filezilla stripped out the line breaks when I uploaded the files and this was what was causing this same error to occur.

By changing the transfer type in Filezilla to Binary (Transfer > Transfer Type > Binary) and re-uploading the wordpress theme, this fixed my issue!

I hope this helps someone!

try extracting your callback function into a separate named function and referring to it by name.

I think you are looking for create_function: http://php.net/manual/en/function.create-function.php

create_function is supported both in php4 and php5

By now this question is mostly obsolete because 5.3 has been around for a long time, but besides the points raised by the other answers, I would like to point out that what you're trying to do can already be done using strtr():

$new = strtr($old, array(
  ':' => '%3A',
  '?' => '%3F',
  '=' => '%3D',
  '&' => '%26',
  '%' => '%25',
));
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!