deprecated preg replace

hello,
how can I fix this error

Deprecated: preg_replace(): The /e modifier is deprecated

return preg_replace('~\{\$(\w+)\}~ie', "isset(\$args['\\1']) ? \$args['\\1'] : '\\0'", $cdata);
LVL 1
XK8ERAsked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
Try:
preg_replace_callback('~\{\$(\w+)\}~i',function($matches){
	global $args;
	return isset($args[$matches[1]]) ? $args[$matches[1]]: $matches[0];
},$cdata);

Open in new window

0
 
käµfm³d 👽Commented:
Is it me, or are you missing a parameter on your preg_replace call? Is $cdata the replacement, or is it the thing being searched within?
0
 
Ray PaseurCommented:
Maybe try removing the e ?  Why did you have the e modifier there?
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
käµfm³d 👽Commented:
@Ray_Paseur

My guess is so that the replacement code (i.e. the isset stuff) would execute as PHP code  ; )
0
 
Ray PaseurCommented:
@kaufmed: Here is what I think the statement says:
$rgx = '~\{\$(\w+)\}~ie';
$txt = "isset(\$args['\\1']) ? \$args['\\1'] : '\\0'";
$new = preg_replace($rgx, $txt, $cdata);
return $new;

Open in new window

Looks like a regex is being used to put a regex into some other string of data.  What that doesn't explain is that the deprecation occurs as of PHP 5.5, however PHP 5.5 is not supported yet - it's a release candidate not to be used in production.
http://php.net/manual/en/function.preg-replace.php
0
 
Terry WoodsConnect With a Mentor IT GuruCommented:
I think this is the equivalent without using the deprecated e modifier:

$pattern = '~\{\$(\w+)\}~i';
$replacement = isset($args['1']) ? $args['1'] : '0';
return preg_replace($pattern, $replacement, $cdata);

Open in new window

0
 
käµfm³d 👽Connect With a Mentor Commented:
@Ray_Paseur

Without the "e" modifier, I would agree with you. However, my understanding of the "e" modifier is that it forces the replacement to be treated as executable code. It's all a matter of XK8ER telling us what the intended goal is.
0
 
Ray PaseurConnect With a Mentor Commented:
@XK8ER: "Deprecated" is not an error; it's just a message indicating that at some time in the future, there may be a removal or change of functionality.  You can suppress the message giving you a while to figure out a permanent solution.  Add the @ to the front of the function name in the call or set error_reporting(^E_DEPRECATED).
0
 
Ray PaseurCommented:
@kaufmed: Agreed.  It would be nice to see the test data and the expected output.  And in any case, PHP offers a solution.
http://php.net/manual/en/function.preg-replace-callback.php
0
 
Terry WoodsIT GuruCommented:
Interesting discussion, and I'm glad you mentioned preg_replace_callback Ray! In this case it doesn't seem to be necessary as far as I can tell.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.