?
Solved

deprecated preg replace

Posted on 2013-06-06
10
Medium Priority
?
386 Views
Last Modified: 2013-06-06
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);
0
Comment
Question by:XK8ER
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39227168
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39227202
Maybe try removing the e ?  Why did you have the e modifier there?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39227209
@Ray_Paseur

My guess is so that the replacement code (i.e. the isset stuff) would execute as PHP code  ; )
0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39227216
@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
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 500 total points
ID: 39227226
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
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 39227228
@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
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 39227235
@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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39227239
@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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39227248
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
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 39227252
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

Featured Post

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question