Preg_replace /e modifier

Since Php 5.4, the /e modifier in preg_replace function has been deprecated and thus the code which uses this modifier should be modified or it will stop working. How can you do that? Simple. Let’s look on the source code of mPDF. We have the following:

$html = preg_replace(‘/\{DATE\s+(.*?)\}/e’,”date(‘\\1’)”,$html );

What this does is replacing for example {DATE d-m-y} with the PHP function of date(‘d-m-y’). We should modify this line as we can see it uses the /e modifier and it won’t work anymore on php 5.5. To replace it, we use the preg_replace_callback function like this:

$html = preg_replace_callback(‘/\{DATE\s+(.*?)\}/’,create_function(‘$matches’, ‘return date($matches[1]);’),$html );

Or even better using PHP 5.5 standards:

$html = preg_replace_callback(‘/\{DATE\s+(.*?)\}/’, function($matches) { return date($matches[1]); },$html );

What this does is that it creates a anonymus function and returns the date() function with the matches.

So, the alternative method to the /e modifier is to use the preg_replace_callback function in Php 5.5 or 5.4.

1 comentarii la acest articol

Lasă un răspuns

Adresa ta de email nu va fi publicată. Câmpurile obligatorii sunt marcate cu *

Acest site folosește Akismet pentru a reduce spamul. Află cum sunt procesate datele comentariilor tale.

Copyright © 2024 toate drepturile
nu sunt
rezervate. Faceti ce vreti, e o tara libera.
Cred ca nu mai are rost sa zic, dar tema e facuta de mine cu Tailwind CSS. Gasesti codul sursa aici.
Inca folosesc WordpPess 🧡. Tema e insa custom Laravel 😎.