FROM preg_replace TO preg_replace_callback

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]);


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_] can be rewritten \w and there're no needs to i modifier


The whole instruction becomes:

$buffer = preg_replace_callback( '#§([\w-]+)\.?([\w-]+)?#', function ($m) { return templ($m[1], $m[2]); }, $buffer );


  • iPhone utility application, connection from FLipSideView to MainView?
  • Any way to get variables keys from jinja2 template string?
  • Using Template::Toolkit with array data from database
  • Concrete range example
  • Iterate Go map get index
  • Why is fread() stopping at a string of 0s?
  • How do I write to a text file in MATLAB?
  • Can I read stream produced by open_memstream()?
  • Infinity loop in while statement
  • reading off multiple CSV files
  • Transfer from Server > Server > Client without download to server
  • How can I distinguish between integer and character when using scanf()
  • Entity framework 'include' including more than it should
  • Kentico Repeater HTML Properties showing with selected transformation
  • How to change display text in item template based on eval in gridview?
  • Zend Framework 2 - Building a simple form with Validators
  • C# DynamicLinq where clause with Any()
  • Migrating from java 1.4 ,EJB 1 to Java 6 and EJB 3 [closed]
  • parallelize process in missForest package
  • How to get google-services.json from Developer console?
  • Read a file in “chunks” using PHP
  • Why is `;;` giving me a syntax error in utop?
  • How to best manage SMTP clients
  • Is there a way to choose which files are displayed to the user via the standard OPENFILE dialogs?
  • Cannot page through all results using nextPageToken on YouTube search API v3
  • Regex for URL rewrite with optional query string parameters
  • C++ Single function pointer for all template instances
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • Regex to match a string not followed by anything
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • Test if a set exists before trying to drop it
  • Django: Count of Group Elements
  • Email format validation in mvc3 view
  • RectangularRangeIndicator format like triangular using dojo
  • Websockets service method fails during R startup
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?