2

Hello fellow netheads!

I'm having issues with updating some old function to preg_replace_callback. Edit: what am I doing wrong?

This is my first (preg_replace/deprecated) function:

if ($handle) {
while (!feof($handle)) {
    $buffer = fgets($handle, 4096);
    @eval('$templ = new '.TEMPL_CLASS.';');
    $buffer = preg_replace("#§([a-z0-9-_]+)\.?([a-z0-9-_]+)?#ie","\$templ->\\1(\\2)",$buffer);
    $out .= $buffer;
    }
fclose($handle);
}

Second function (this is my attempt at converting to preg_replace_callback):

if ($handle) {
  while (!feof($handle)) {
    $buffer = fgets($handle, 4096);
    @eval('$templ = new '.TEMPL_CLASS.';');  
    $buffer = preg_replace_callback(
      '#§([\w-]+)\.?([\w-]+)?#',
      function ($m) {
        @$templ->$m[1]($m[2]);   
      },
      $buffer
    );
    $out .= $buffer;
  }
  fclose($handle);
}

OLD! M42's answear fixed the follow error:

Warning: preg_replace_callback(): Modifier /e cannot be used with replacement callback in /var/www/inc/engine.php on line 52

); <-- line 52
$out .= $buffer;

Edit: I dont know how to handle the render part of this..

$buffer = preg_replace("#§([a-z0-9-_]+)\.?([a-z0-9-_]+)?#ie","\$templ->\\1(\\2)",$buffer);

Right now it is rendering a blank page.. I guess the error is in

return templ($m[1], $m[2]);

mlv
  • 55
  • 2
  • 7
  • 1
    Sorry, what is the question? – e h Mar 20 '14 at 10:34
  • Oh, thanks for noticing! I don't know what I'm doing wrong in my attempt (the second codeblock) – mlv Mar 20 '14 at 10:41
  • OK, are you getting errors? If so what are they? Please add them to the question, it will make it easier for us to help you. – e h Mar 20 '14 at 11:26
  • Sorry.. I just added the error to my original post :) thank you for taking your time with my problem – mlv Mar 20 '14 at 12:20

1 Answers1

4

As it's said in the message, removed the e modifier:

'#§\\(\\[a-z0-9-_\\]+\\)\.?\\(\\[a-z0-9-_\\]+\\)?#i'
//                                         here ___^

And there're no needs to escape all these characters:

'#§([a-z0-9_-]+)\.?([a-z0-9_-]+)?#i'

[a-z0-9_] can be rewritten \w and there're no needs to i modifier

'#§([\w-]+)\.?([\w-]+)?#'

The whole instruction becomes:

$buffer = preg_replace_callback(
  '#§([\w-]+)\.?([\w-]+)?#',
  function ($m) {
    return templ($m[1], $m[2]);
  },
  $buffer
);
Toto
  • 89,455
  • 62
  • 89
  • 125
  • That worked fine! Atleast it threw me a new error.. Sorry I cant vote you up just yet, I need more rep – mlv Mar 20 '14 at 12:27
  • Now it throws me: Warning: preg_replace_callback() expects at least 3 parameters, 2 given in /var/www/inc/engine.php on line 52 – mlv Mar 20 '14 at 12:32
  • 1
    @mlv: I've edited with the whole instruction. It works pretty fine for me. May be you're using it wrong. – Toto Mar 20 '14 at 12:50
  • 1
    Thank you! It wasnt working because I was trying to return templ as a variable instead of a function ... return @templ ... – mlv Mar 20 '14 at 12:57
  • This doesnt solve the issue tho, just that error (thank you for helping me with that) – mlv Mar 20 '14 at 14:29