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;

?>
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
acrs
  • 563
  • 2
  • 8
  • 16

6 Answers6

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);
}
user229044
  • 232,980
  • 40
  • 330
  • 338
  • 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` – acrs Feb 09 '11 at 19:56
  • @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: http://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. – Felix Kling Feb 09 '11 at 20:14
  • @user Try just writing a regular function and passing it in as a string. – user229044 Feb 09 '11 at 20:17
  • 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 M. Feb 09 '11 at 20:21
  • @Scott I'm talking about a [callback](http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback), the same thing you'd pass to `call_user_func`. It has nothing to do with `eval`. – user229044 Feb 09 '11 at 20:27
  • @Felix I'm new to php i read the documentation and i did not understand it. i didn't make the scrip that I'm trying to use just made modifications to it so can someone give me an example – acrs Feb 09 '11 at 20:28
  • ah ok. I misunderstood. yeah that makes perfect sense then. – Scott M. Feb 09 '11 at 20:29
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!

Pakage
  • 684
  • 7
  • 12
4

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

Scott M.
  • 7,313
  • 30
  • 39
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

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
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',
));
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
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..

Kreshnik
  • 2,661
  • 5
  • 31
  • 39