解析错误:语法错误,意外的T_FUNCTION第10行? [英] Parse error: syntax error, unexpected T_FUNCTION line 10?

查看:117
本文介绍了解析错误:语法错误,意外的T_FUNCTION第10行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有什么问题?我在测试服务器上运行了代码,并且代码可以正常工作,但是当我将其上传到生产服务器时,我得到了

Parse error: syntax error, unexpected T_FUNCTION in /hermes/bosweb/web013/b130/ipg.acrsflcom/darayngedbeats/gentest.php on line 10

这是我的代码

$old = "http://darayngedbeats1.s3.amazonaws.com    /mp3/CrazyMonsta2.mp3?AWSAccessKeyId=AKIAJXA36ESCLQHCB54Q&Expires=1297279906& Signature=HD36ZQE8yeTIW6JPWKMcciPTiTs%3D"; //enter the key that needs to be converted
$search =  array(":","?","=","&","%");
$replace = array("%3A","%3F","%3D","%26","%25");

function search_replace($s,$r,$sql)
{ $e = '/('.implode('|',array_map('preg_quote', $s)).')/';
  $r = array_combine($s,$r);
  return preg_replace_callback($e, function($v) use ($s,$r) { return $r[$v[1]];  },$sql);
}

echo "<br><br>";
$new = search_replace($search,$replace,$old);
echo $new;

?>

解决方案

该错误很可能是由

引起的

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

您有可能使用的是PHP 5.2或更早版本,该版本不支持闭包.您可以使用 phpinfo() 找出正在使用的PHP版本. /p>

您可能需要升级到PHP 5.3+,或使用 create_function ,或编写静态函数并将其作为回调.

这是最后一个选项的示例,它使用一个简单的类来存储$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);
}

What is wrong with my code? I ran the code on my test server and the code worked but when I upload it to my production server I get

Parse error: syntax error, unexpected T_FUNCTION in /hermes/bosweb/web013/b130/ipg.acrsflcom/darayngedbeats/gentest.php on line 10

here is my code

$old = "http://darayngedbeats1.s3.amazonaws.com    /mp3/CrazyMonsta2.mp3?AWSAccessKeyId=AKIAJXA36ESCLQHCB54Q&Expires=1297279906& Signature=HD36ZQE8yeTIW6JPWKMcciPTiTs%3D"; //enter the key that needs to be converted
$search =  array(":","?","=","&","%");
$replace = array("%3A","%3F","%3D","%26","%25");

function search_replace($s,$r,$sql)
{ $e = '/('.implode('|',array_map('preg_quote', $s)).')/';
  $r = array_combine($s,$r);
  return preg_replace_callback($e, function($v) use ($s,$r) { return $r[$v[1]];  },$sql);
}

echo "<br><br>";
$new = search_replace($search,$replace,$old);
echo $new;

?>

解决方案

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

这篇关于解析错误:语法错误,意外的T_FUNCTION第10行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆