Replace preg_replace() e modifier with preg_replace_callback Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Replace deprecated preg_replace /e with preg_replace_callbackCan someone explain the /e regex modifier?Replace preg_replace() to preg_replace_callback()PHP7 - The /e modifier is no longer supported, use preg_replace_callback instead/e modifier is deprecatedPHP preg_replace: DeprecatedPHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback insteadCode Repair, preg_replace with /e DEPRECATEDThe /e modifier is deprecated, use preg_replace_callback instead - wordpress pluginChange preg_replace into preg_replace_callbackHow to do a regular expression replace in MySQL?How to replace all occurrences of a string in JavaScriptReference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loopsReplace preg_replace modifier with preg_replace_callbackPreg_Replace .. e modifier deprecatedCan't convert preg_replace() with modifier /e to preg_replace_callback()Change preg_replace into preg_replace_callbackphp converting preg_replace to preg_replace_callback
How can I prevent/balance waiting and turtling as a response to cooldown mechanics
"Destructive power" carried by a B-52?
How does TikZ render an arc?
Why is there so little support for joining EFTA in the British parliament?
Why are two-digit numbers in Jonathan Swift's "Gulliver's Travels" (1726) written in "German style"?
How to make an animal which can only breed for a certain number of generations?
Are there any irrational/transcendental numbers for which the distribution of decimal digits is not uniform?
What should one know about term logic before studying propositional and predicate logic?
NIntegrate on a solution of a matrix ODE
What is "Lambda" in Heston's original paper on stochastic volatility models?
Besides transaction validation, are there any other uses of the Script language in Bitcoin
Why can't fire hurt Daenerys but it did to Jon Snow in season 1?
Do i imagine the linear (straight line) homotopy in a correct way?
How to achieve cat-like agility?
Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?
Does a random sequence of vectors span a Hilbert space?
French equivalents of おしゃれは足元から (Every good outfit starts with the shoes)
How do I find my Spellcasting Ability for my D&D character?
Why did Bronn offer to be Tyrion Lannister's champion in trial by combat?
Where did Ptolemy compare the Earth to the distance of fixed stars?
What is the proper term for etching or digging of wall to hide conduit of cables
Is there a spell that can create a permanent fire?
Plotting a Maclaurin series
How to infer difference of population proportion between two groups when proportion is small?
Replace preg_replace() e modifier with preg_replace_callback
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Replace deprecated preg_replace /e with preg_replace_callbackCan someone explain the /e regex modifier?Replace preg_replace() to preg_replace_callback()PHP7 - The /e modifier is no longer supported, use preg_replace_callback instead/e modifier is deprecatedPHP preg_replace: DeprecatedPHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback insteadCode Repair, preg_replace with /e DEPRECATEDThe /e modifier is deprecated, use preg_replace_callback instead - wordpress pluginChange preg_replace into preg_replace_callbackHow to do a regular expression replace in MySQL?How to replace all occurrences of a string in JavaScriptReference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loopsReplace preg_replace modifier with preg_replace_callbackPreg_Replace .. e modifier deprecatedCan't convert preg_replace() with modifier /e to preg_replace_callback()Change preg_replace into preg_replace_callbackphp converting preg_replace to preg_replace_callback
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm terrible with regular expressions. I'm trying to replace this:
public static function camelize($word) _)([a-z])/e', 'strtoupper("\2")', $word);
with preg_replace_callback with an anonymous function. I don't understand what the \2 is doing. Or for that matter exactly how preg_replace_callback works.
What would be the correct code for achieving this?
php regex preg-replace preg-replace-callback
|
show 2 more comments
I'm terrible with regular expressions. I'm trying to replace this:
public static function camelize($word) _)([a-z])/e', 'strtoupper("\2")', $word);
with preg_replace_callback with an anonymous function. I don't understand what the \2 is doing. Or for that matter exactly how preg_replace_callback works.
What would be the correct code for achieving this?
php regex preg-replace preg-replace-callback
1
The e modifier is deprecated as of PHP 5.5.0
– HamZa
Mar 16 '13 at 20:17
8
@HamZaDzCyberDeV I know. That's one of the reasons I want to replace it with preg_replace_callback
– Casey
Mar 16 '13 at 20:18
2
There's a manual page forpreg_replace_callback
. And\2
will become$matches[2]
in said callback. Or which part are you confused about specifically?
– mario
Mar 16 '13 at 20:20
@mario ahh The $matches[2] was all I needed. I still don't understand how it works, but it does. If you put that in an answer I'll mark it is as solving the problem.
– Casey
Mar 16 '13 at 20:28
3
Please don't usecreate_function
, it's just yet another wrapper aroundeval
. You should use a proper anonymous function, unless you're stuck in PHP 5.2 for some reason.
– IMSoP
Mar 16 '13 at 20:35
|
show 2 more comments
I'm terrible with regular expressions. I'm trying to replace this:
public static function camelize($word) _)([a-z])/e', 'strtoupper("\2")', $word);
with preg_replace_callback with an anonymous function. I don't understand what the \2 is doing. Or for that matter exactly how preg_replace_callback works.
What would be the correct code for achieving this?
php regex preg-replace preg-replace-callback
I'm terrible with regular expressions. I'm trying to replace this:
public static function camelize($word) _)([a-z])/e', 'strtoupper("\2")', $word);
with preg_replace_callback with an anonymous function. I don't understand what the \2 is doing. Or for that matter exactly how preg_replace_callback works.
What would be the correct code for achieving this?
php regex preg-replace preg-replace-callback
php regex preg-replace preg-replace-callback
edited May 11 '16 at 7:44
Rizier123
52.5k1567108
52.5k1567108
asked Mar 16 '13 at 20:13
CaseyCasey
1,15131227
1,15131227
1
The e modifier is deprecated as of PHP 5.5.0
– HamZa
Mar 16 '13 at 20:17
8
@HamZaDzCyberDeV I know. That's one of the reasons I want to replace it with preg_replace_callback
– Casey
Mar 16 '13 at 20:18
2
There's a manual page forpreg_replace_callback
. And\2
will become$matches[2]
in said callback. Or which part are you confused about specifically?
– mario
Mar 16 '13 at 20:20
@mario ahh The $matches[2] was all I needed. I still don't understand how it works, but it does. If you put that in an answer I'll mark it is as solving the problem.
– Casey
Mar 16 '13 at 20:28
3
Please don't usecreate_function
, it's just yet another wrapper aroundeval
. You should use a proper anonymous function, unless you're stuck in PHP 5.2 for some reason.
– IMSoP
Mar 16 '13 at 20:35
|
show 2 more comments
1
The e modifier is deprecated as of PHP 5.5.0
– HamZa
Mar 16 '13 at 20:17
8
@HamZaDzCyberDeV I know. That's one of the reasons I want to replace it with preg_replace_callback
– Casey
Mar 16 '13 at 20:18
2
There's a manual page forpreg_replace_callback
. And\2
will become$matches[2]
in said callback. Or which part are you confused about specifically?
– mario
Mar 16 '13 at 20:20
@mario ahh The $matches[2] was all I needed. I still don't understand how it works, but it does. If you put that in an answer I'll mark it is as solving the problem.
– Casey
Mar 16 '13 at 20:28
3
Please don't usecreate_function
, it's just yet another wrapper aroundeval
. You should use a proper anonymous function, unless you're stuck in PHP 5.2 for some reason.
– IMSoP
Mar 16 '13 at 20:35
1
1
The e modifier is deprecated as of PHP 5.5.0
– HamZa
Mar 16 '13 at 20:17
The e modifier is deprecated as of PHP 5.5.0
– HamZa
Mar 16 '13 at 20:17
8
8
@HamZaDzCyberDeV I know. That's one of the reasons I want to replace it with preg_replace_callback
– Casey
Mar 16 '13 at 20:18
@HamZaDzCyberDeV I know. That's one of the reasons I want to replace it with preg_replace_callback
– Casey
Mar 16 '13 at 20:18
2
2
There's a manual page for
preg_replace_callback
. And \2
will become $matches[2]
in said callback. Or which part are you confused about specifically?– mario
Mar 16 '13 at 20:20
There's a manual page for
preg_replace_callback
. And \2
will become $matches[2]
in said callback. Or which part are you confused about specifically?– mario
Mar 16 '13 at 20:20
@mario ahh The $matches[2] was all I needed. I still don't understand how it works, but it does. If you put that in an answer I'll mark it is as solving the problem.
– Casey
Mar 16 '13 at 20:28
@mario ahh The $matches[2] was all I needed. I still don't understand how it works, but it does. If you put that in an answer I'll mark it is as solving the problem.
– Casey
Mar 16 '13 at 20:28
3
3
Please don't use
create_function
, it's just yet another wrapper around eval
. You should use a proper anonymous function, unless you're stuck in PHP 5.2 for some reason.– IMSoP
Mar 16 '13 at 20:35
Please don't use
create_function
, it's just yet another wrapper around eval
. You should use a proper anonymous function, unless you're stuck in PHP 5.2 for some reason.– IMSoP
Mar 16 '13 at 20:35
|
show 2 more comments
1 Answer
1
active
oldest
votes
In a regular expression, you can "capture" parts of the matched string with (brackets)
; in this case, you are capturing the (^|_)
and ([a-z])
parts of the match. These are numbered starting at 1, so you have back-references 1 and 2. Match 0 is the whole matched string.
The /e
modifier takes a replacement string, and substitutes backslash followed by a number (e.g. 1
) with the appropriate back-reference - but because you're inside a string, you need to escape the backslash, so you get '\1'
. It then (effectively) runs eval
to run the resulting string as though it was PHP code (which is why it's being deprecated, because it's easy to use eval
in an insecure way).
The preg_replace_callback
function instead takes a callback function and passes it an array containing the matched back-references. So where you would have written '\1'
, you instead access element 1 of that parameter - e.g. if you have an anonymous function of the form function($matches) ...
, the first back-reference is $matches[1]
inside that function.
So a /e
argument of
'do_stuff(\1) . "and" . do_stuff(\2)'
could become a callback of
function($m) return do_stuff($m[1]) . "and" . do_stuff($m[2]);
Or in your case
'strtoupper("\2")'
could become
function($m) return strtoupper($m[2]);
Note that $m
and $matches
are not magic names, they're just the parameter name I gave when declaring my callback functions. Also, you don't have to pass an anonymous function, it could be a function name as a string, or something of the form array($object, $method)
, as with any callback in PHP, e.g.
function stuffy_callback($things)
return do_stuff($things[1]) . "and" . do_stuff($things[2]);
$foo = preg_replace_callback('/([a-z]+) and ([a-z]+)/', 'stuffy_callback', 'fish and chips');
As with any function, you can't access variables outside your callback (from the surrounding scope) by default. When using an anonymous function, you can use the use
keyword to import the variables you need to access, as discussed in the PHP manual. e.g. if the old argument was
'do_stuff(\1, $foo)'
then the new callback might look like
function($m) use ($foo) return do_stuff($m[1], $foo);
Gotchas
- Use of
preg_replace_callback
is instead of the/e
modifier on the regex, so you need to remove that flag from your "pattern" argument. So a pattern like/blah(.*)blah/mei
would become/blah(.*)blah/mi
. - The
/e
modifier used a variant ofaddslashes()
internally on the arguments, so some replacements usedstripslashes()
to remove it; in most cases, you probably want to remove the call tostripslashes
from your new callback.
add a comment |
protected by Community♦ Jun 1 '16 at 6:44
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In a regular expression, you can "capture" parts of the matched string with (brackets)
; in this case, you are capturing the (^|_)
and ([a-z])
parts of the match. These are numbered starting at 1, so you have back-references 1 and 2. Match 0 is the whole matched string.
The /e
modifier takes a replacement string, and substitutes backslash followed by a number (e.g. 1
) with the appropriate back-reference - but because you're inside a string, you need to escape the backslash, so you get '\1'
. It then (effectively) runs eval
to run the resulting string as though it was PHP code (which is why it's being deprecated, because it's easy to use eval
in an insecure way).
The preg_replace_callback
function instead takes a callback function and passes it an array containing the matched back-references. So where you would have written '\1'
, you instead access element 1 of that parameter - e.g. if you have an anonymous function of the form function($matches) ...
, the first back-reference is $matches[1]
inside that function.
So a /e
argument of
'do_stuff(\1) . "and" . do_stuff(\2)'
could become a callback of
function($m) return do_stuff($m[1]) . "and" . do_stuff($m[2]);
Or in your case
'strtoupper("\2")'
could become
function($m) return strtoupper($m[2]);
Note that $m
and $matches
are not magic names, they're just the parameter name I gave when declaring my callback functions. Also, you don't have to pass an anonymous function, it could be a function name as a string, or something of the form array($object, $method)
, as with any callback in PHP, e.g.
function stuffy_callback($things)
return do_stuff($things[1]) . "and" . do_stuff($things[2]);
$foo = preg_replace_callback('/([a-z]+) and ([a-z]+)/', 'stuffy_callback', 'fish and chips');
As with any function, you can't access variables outside your callback (from the surrounding scope) by default. When using an anonymous function, you can use the use
keyword to import the variables you need to access, as discussed in the PHP manual. e.g. if the old argument was
'do_stuff(\1, $foo)'
then the new callback might look like
function($m) use ($foo) return do_stuff($m[1], $foo);
Gotchas
- Use of
preg_replace_callback
is instead of the/e
modifier on the regex, so you need to remove that flag from your "pattern" argument. So a pattern like/blah(.*)blah/mei
would become/blah(.*)blah/mi
. - The
/e
modifier used a variant ofaddslashes()
internally on the arguments, so some replacements usedstripslashes()
to remove it; in most cases, you probably want to remove the call tostripslashes
from your new callback.
add a comment |
In a regular expression, you can "capture" parts of the matched string with (brackets)
; in this case, you are capturing the (^|_)
and ([a-z])
parts of the match. These are numbered starting at 1, so you have back-references 1 and 2. Match 0 is the whole matched string.
The /e
modifier takes a replacement string, and substitutes backslash followed by a number (e.g. 1
) with the appropriate back-reference - but because you're inside a string, you need to escape the backslash, so you get '\1'
. It then (effectively) runs eval
to run the resulting string as though it was PHP code (which is why it's being deprecated, because it's easy to use eval
in an insecure way).
The preg_replace_callback
function instead takes a callback function and passes it an array containing the matched back-references. So where you would have written '\1'
, you instead access element 1 of that parameter - e.g. if you have an anonymous function of the form function($matches) ...
, the first back-reference is $matches[1]
inside that function.
So a /e
argument of
'do_stuff(\1) . "and" . do_stuff(\2)'
could become a callback of
function($m) return do_stuff($m[1]) . "and" . do_stuff($m[2]);
Or in your case
'strtoupper("\2")'
could become
function($m) return strtoupper($m[2]);
Note that $m
and $matches
are not magic names, they're just the parameter name I gave when declaring my callback functions. Also, you don't have to pass an anonymous function, it could be a function name as a string, or something of the form array($object, $method)
, as with any callback in PHP, e.g.
function stuffy_callback($things)
return do_stuff($things[1]) . "and" . do_stuff($things[2]);
$foo = preg_replace_callback('/([a-z]+) and ([a-z]+)/', 'stuffy_callback', 'fish and chips');
As with any function, you can't access variables outside your callback (from the surrounding scope) by default. When using an anonymous function, you can use the use
keyword to import the variables you need to access, as discussed in the PHP manual. e.g. if the old argument was
'do_stuff(\1, $foo)'
then the new callback might look like
function($m) use ($foo) return do_stuff($m[1], $foo);
Gotchas
- Use of
preg_replace_callback
is instead of the/e
modifier on the regex, so you need to remove that flag from your "pattern" argument. So a pattern like/blah(.*)blah/mei
would become/blah(.*)blah/mi
. - The
/e
modifier used a variant ofaddslashes()
internally on the arguments, so some replacements usedstripslashes()
to remove it; in most cases, you probably want to remove the call tostripslashes
from your new callback.
add a comment |
In a regular expression, you can "capture" parts of the matched string with (brackets)
; in this case, you are capturing the (^|_)
and ([a-z])
parts of the match. These are numbered starting at 1, so you have back-references 1 and 2. Match 0 is the whole matched string.
The /e
modifier takes a replacement string, and substitutes backslash followed by a number (e.g. 1
) with the appropriate back-reference - but because you're inside a string, you need to escape the backslash, so you get '\1'
. It then (effectively) runs eval
to run the resulting string as though it was PHP code (which is why it's being deprecated, because it's easy to use eval
in an insecure way).
The preg_replace_callback
function instead takes a callback function and passes it an array containing the matched back-references. So where you would have written '\1'
, you instead access element 1 of that parameter - e.g. if you have an anonymous function of the form function($matches) ...
, the first back-reference is $matches[1]
inside that function.
So a /e
argument of
'do_stuff(\1) . "and" . do_stuff(\2)'
could become a callback of
function($m) return do_stuff($m[1]) . "and" . do_stuff($m[2]);
Or in your case
'strtoupper("\2")'
could become
function($m) return strtoupper($m[2]);
Note that $m
and $matches
are not magic names, they're just the parameter name I gave when declaring my callback functions. Also, you don't have to pass an anonymous function, it could be a function name as a string, or something of the form array($object, $method)
, as with any callback in PHP, e.g.
function stuffy_callback($things)
return do_stuff($things[1]) . "and" . do_stuff($things[2]);
$foo = preg_replace_callback('/([a-z]+) and ([a-z]+)/', 'stuffy_callback', 'fish and chips');
As with any function, you can't access variables outside your callback (from the surrounding scope) by default. When using an anonymous function, you can use the use
keyword to import the variables you need to access, as discussed in the PHP manual. e.g. if the old argument was
'do_stuff(\1, $foo)'
then the new callback might look like
function($m) use ($foo) return do_stuff($m[1], $foo);
Gotchas
- Use of
preg_replace_callback
is instead of the/e
modifier on the regex, so you need to remove that flag from your "pattern" argument. So a pattern like/blah(.*)blah/mei
would become/blah(.*)blah/mi
. - The
/e
modifier used a variant ofaddslashes()
internally on the arguments, so some replacements usedstripslashes()
to remove it; in most cases, you probably want to remove the call tostripslashes
from your new callback.
In a regular expression, you can "capture" parts of the matched string with (brackets)
; in this case, you are capturing the (^|_)
and ([a-z])
parts of the match. These are numbered starting at 1, so you have back-references 1 and 2. Match 0 is the whole matched string.
The /e
modifier takes a replacement string, and substitutes backslash followed by a number (e.g. 1
) with the appropriate back-reference - but because you're inside a string, you need to escape the backslash, so you get '\1'
. It then (effectively) runs eval
to run the resulting string as though it was PHP code (which is why it's being deprecated, because it's easy to use eval
in an insecure way).
The preg_replace_callback
function instead takes a callback function and passes it an array containing the matched back-references. So where you would have written '\1'
, you instead access element 1 of that parameter - e.g. if you have an anonymous function of the form function($matches) ...
, the first back-reference is $matches[1]
inside that function.
So a /e
argument of
'do_stuff(\1) . "and" . do_stuff(\2)'
could become a callback of
function($m) return do_stuff($m[1]) . "and" . do_stuff($m[2]);
Or in your case
'strtoupper("\2")'
could become
function($m) return strtoupper($m[2]);
Note that $m
and $matches
are not magic names, they're just the parameter name I gave when declaring my callback functions. Also, you don't have to pass an anonymous function, it could be a function name as a string, or something of the form array($object, $method)
, as with any callback in PHP, e.g.
function stuffy_callback($things)
return do_stuff($things[1]) . "and" . do_stuff($things[2]);
$foo = preg_replace_callback('/([a-z]+) and ([a-z]+)/', 'stuffy_callback', 'fish and chips');
As with any function, you can't access variables outside your callback (from the surrounding scope) by default. When using an anonymous function, you can use the use
keyword to import the variables you need to access, as discussed in the PHP manual. e.g. if the old argument was
'do_stuff(\1, $foo)'
then the new callback might look like
function($m) use ($foo) return do_stuff($m[1], $foo);
Gotchas
- Use of
preg_replace_callback
is instead of the/e
modifier on the regex, so you need to remove that flag from your "pattern" argument. So a pattern like/blah(.*)blah/mei
would become/blah(.*)blah/mi
. - The
/e
modifier used a variant ofaddslashes()
internally on the arguments, so some replacements usedstripslashes()
to remove it; in most cases, you probably want to remove the call tostripslashes
from your new callback.
edited Dec 21 '17 at 18:22
answered Mar 16 '13 at 20:37
IMSoPIMSoP
48.5k65895
48.5k65895
add a comment |
add a comment |
protected by Community♦ Jun 1 '16 at 6:44
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
1
The e modifier is deprecated as of PHP 5.5.0
– HamZa
Mar 16 '13 at 20:17
8
@HamZaDzCyberDeV I know. That's one of the reasons I want to replace it with preg_replace_callback
– Casey
Mar 16 '13 at 20:18
2
There's a manual page for
preg_replace_callback
. And\2
will become$matches[2]
in said callback. Or which part are you confused about specifically?– mario
Mar 16 '13 at 20:20
@mario ahh The $matches[2] was all I needed. I still don't understand how it works, but it does. If you put that in an answer I'll mark it is as solving the problem.
– Casey
Mar 16 '13 at 20:28
3
Please don't use
create_function
, it's just yet another wrapper aroundeval
. You should use a proper anonymous function, unless you're stuck in PHP 5.2 for some reason.– IMSoP
Mar 16 '13 at 20:35