17

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;

?>
4
  • i presume it has to do with the callback function Commented Feb 9, 2011 at 19:37
  • 1
    line 7 return preg_replace_callback($e, function($v) use ($s,$r) { return $r[$v[1]]; },$sql); Commented Feb 9, 2011 at 19:39
  • Q: "Which line is line 10?" - A: "line 7 [code]" I lol'd. Commented Feb 9, 2011 at 19:51
  • line 7 is 10 return preg_replace_callback Commented Feb 9, 2011 at 19:55

6 Answers 6

32

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);
}
Sign up to request clarification or add additional context in comments.

7 Comments

ok i tryed this $newfunc = preg_replace_callback($e, create_function($v) use ($s,$r) { return $r[$v[1]]; },$sql); now i get Parse error: syntax error, unexpected T_USE in /hermes/bosweb/web013/b130/ipg.acrsflcom/darayngedbeats/gentest.php on line 10
@user514584: use does not exist before PHP 5.3 either. You should have read the documentation @meagar linked to, to see how create_function works. Here again: php.net/manual/en/function.create-function.php. Unfortunately, you cannot create closures with create_function so you have to think about a different way how to access $r and $e in the callback.
@user Try just writing a regular function and passing it in as a string.
doesn't passing a function in as a string use eval()? eval is evil as i recall. It opens too many security holes. Just extract and name it.
@Scott I'm talking about a callback, the same thing you'd pass to call_user_func. It has nothing to do with eval.
|
12

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!

2 Comments

Thank you so much for this!! Would never have thought to try that but it did indeed fix my 500 errors :)
This was a godsend for me. I was looking for HOURS trying to find the answer and this finally fixed it. Thank you so much for posting this!!
4

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

Comments

3

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

Comments

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():

$new = strtr($old, array(
  ':' => '%3A',
  '?' => '%3F',
  '=' => '%3D',
  '&' => '%26',
  '%' => '%25',
));

Comments

1
PHP Parse error:  syntax error, unexpected 'function' (T_FUNCTION)

In my case, I fixed this error, by adding a semicolon that I've forgotten at the end of a variable declared previously..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.