Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi everyone can someone please tell me what 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;

?>
share|improve this question
    
Which line is line 10? –  Daniel A. White Feb 9 '11 at 19:36
    
what version of php? –  Foo Bah Feb 9 '11 at 19:36
    
i presume it has to do with the callback function –  Foo Bah Feb 9 '11 at 19:37
    
line 7 return preg_replace_callback($e, function($v) use ($s,$r) { return $r[$v[1]]; },$sql); –  acrs Feb 9 '11 at 19:39
    
Q: "Which line is line 10?" - A: "line 7 [code]" I lol'd. –  Jefffrey Feb 9 '11 at 19:51

4 Answers 4

up vote 17 down vote accepted

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);
}
share|improve this answer
    
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 9 '11 at 19:56
    
@meagar what did i do wrong –  acrs Feb 9 '11 at 20:02
    
@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. –  Felix Kling Feb 9 '11 at 20:14
    
@user Try just writing a regular function and passing it in as a string. –  meagar Feb 9 '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 9 '11 at 20:21

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

share|improve this answer

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

share|improve this answer

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',
));
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.