0

I have the following piece of code: http://pastebin.com/VaZkRxSu. And I am trying to update the functions preg_replace with preg_replace_callback. I don't understand what would be the callback in my case. A callback that will be called and passed an array of matched elements in the subject string. The callback should return the replacement string. This is the callback signature: string handler ( array $matches ) - This is what it says on php.net. Issue is located on following lines:

Line 160

$file_contents = preg_replace("!{$ldq}\*.*?\*{$rdq}!se","",$file_contents);

In this case preg_replace replacement is an empty field "". I have tried just changing the preg_replace to preg_replace_callback, but I get error: (2) preg_replace_callback(): Requires argument 2, '', to be a valid callback.

Line 163

$file_contents = preg_replace('%(<\?(?!php|=|$))%i', '<?php echo \'\\1\'?>'."\n", $file_contents);

Line 168

$file_contents = preg_replace("!{$ldq}\s*literal\s*{$rdq}(.*?){$ldq}\s*/literal\s*{$rdq}!s", stripslashes($ldq . "literal" . $rdq), $file_contents);

Line 173

$file_contents = preg_replace("!{$ldq}\s*php\s*{$rdq}(.*?){$ldq}\s*/php\s*{$rdq}!s", stripslashes($ldq . "php" . $rdq), $file_contents);

Line 196

$text[$i+1] = preg_replace('~^(\r\n|\r|\n)~', '', $text[$i+1]);

Line 208

$compiled_text = preg_replace('!\?>\n?<\?php!', '', $compiled_text);

I also tried adding as the callback a function($m) like here

Please help me on this, I am a noob and doing my best on learning php.

Community
  • 1
  • 1
Adrian
  • 2,273
  • 4
  • 38
  • 78
  • @mario I checked that question too, but the second argument I don't think is dynamic or null like in my case. – Adrian May 03 '14 at 14:46
  • `preg_replace` itself isn't deprecated. It's only the `/e` eval modifier. Hence does not apply to your empty-replacement plain `preg_replace` calls. – mario May 03 '14 at 14:48
  • @mario, I see so I can use `preg_replace` without the e modifier, and how to do that? – Adrian May 03 '14 at 15:02
  • You're already doing so in five out of six of your examples. – mario May 03 '14 at 15:59
  • @mario hmm, I am really trying to figure it out. But can't see which is the modifier. – Adrian May 03 '14 at 16:07
  • 2
    They are `!se` (probably needlessly including the eval modifier) in your first example, and `%i` in the second, `!i` in the third and fourth example. The other calls do not contain any letter (=modifier) after the regex delimiter. – mario May 03 '14 at 16:10
  • I see now, deleted `se` and it;s working perfectly. Thank you Mario. – Adrian May 03 '14 at 16:17

0 Answers0