php 解析错误:语法错误,意外的 T_FUNCTION 第 10 行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4949573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse error: syntax error, unexpected T_FUNCTION line 10?
提问by acrs
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;
?>
回答by meagar
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()
.
您可能正在使用不支持闭包的 PHP 5.2 或更早版本。您可以找出您正在使用的 PHP 版本 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.
您可能需要升级到 PHP 5.3+,或者使用create_function
,或者编写一个静态函数并将其作为回调传递。
Here's an example of the last option, using a simple class to store the state of $r
:
这是最后一个选项的示例,使用一个简单的类来存储 的状态$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);
}
回答by Pakage
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.
对于在 PHP 5.3+ 上遇到此错误的任何人,尤其是使用 wordpress 主题时,我建议您查看服务器上实际文件的格式。
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.
当我遇到这个错误并查看在服务器上抛出错误的 PHP 文件时,它们没有换行符并且被有效地缩小到一行。
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.
出于某种原因,Filezilla 在我上传文件时去掉了换行符,这就是导致发生同样错误的原因。
By changing the transfer type in Filezilla to Binary (Transfer > Transfer Type > Binary) and re-uploading the wordpress theme, this fixed my issue!
通过将 Filezilla 中的传输类型更改为二进制(传输 > 传输类型 > 二进制)并重新上传 wordpress 主题,这解决了我的问题!
I hope this helps someone!
我希望这可以帮助别人!
回答by Scott M.
try extracting your callback function into a separate named function and referring to it by name.
尝试将您的回调函数提取到一个单独的命名函数中并按名称引用它。
回答by Foo Bah
I think you are looking for create_function: http://php.net/manual/en/function.create-function.php
我认为您正在寻找 create_function:http: //php.net/manual/en/function.create-function.php
create_function is supported both in php4 and php5
php4 和 php5 都支持 create_function
回答by Ja?ck
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()
:
到目前为止,这个问题大部分已经过时了,因为 5.3 已经存在很长时间了,但是除了其他答案提出的要点之外,我想指出您已经可以使用strtr()
以下方法完成您想要做的事情:
$new = strtr($old, array(
':' => '%3A',
'?' => '%3F',
'=' => '%3D',
'&' => '%26',
'%' => '%25',
));