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;
?>
5 个解决方案
#1
29
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或更早版本,它不支持闭包。您可以找到您正在使用phpinfo()的PHP版本。
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);
}
#2
11
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!
我希望这可以帮助别人!
#3
4
try extracting your callback function into a separate named function and referring to it by name.
尝试将回调函数提取到单独的命名函数中并按名称引用它。
#4
3
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
#5
2
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',
));
#1
29
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或更早版本,它不支持闭包。您可以找到您正在使用phpinfo()的PHP版本。
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);
}
#2
11
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!
我希望这可以帮助别人!
#3
4
try extracting your callback function into a separate named function and referring to it by name.
尝试将回调函数提取到单独的命名函数中并按名称引用它。
#4
3
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
#5
2
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',
));