Questions tagged [preg-replace-callback]

preg_replace_callback() is a PHP function that uses regular expressions to find substrings and a PHP callback function to perform the replacement.

preg_replace_callback replaces the older (and insecure) /e flag in preg_replace, where PHP would eval the replace string and execute the PHP contained inside. preg_replace_callback uses a direct function call instead, which negates that risk entirely.

The most common use is where you need to use a regular expression to find strings, but you need a PHP function to transform that string. Consider the below function, which finds instances of Bob and makes them all uppercase. This example passes an anonymous function (PHP >= 5.3) but you can pass the function name instead.

echo preg_replace_callback('/Bob/', function($match) {
    return strtoupper($match[0]);
}, 'We like Bob');
// outputs We like BOB
412 questions
-2
votes
1 answer

php regex to replace spaces in braces including braces

I wonder if it's possible to solve such kind of template parsing using RegEx-es I have text similar to: 'Hello, {{ Na me|{{Surname }}}}. Would you like some drink? {{CoffeePreferred? We have a special discount for coffee}} {{Tea Preferred? We have a…
sly
  • 149
  • 1
  • 6
-2
votes
1 answer

preg_replace deprecated use preg_replace_callback

I am getting preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in one of the files in my application. Near the below lines $str = preg_replace('/\&\#([0-9]+)\;/me', "code2utf('\\1',{$lo})",$str); $str =…
rji rji
  • 697
  • 3
  • 17
  • 37
-2
votes
2 answers

How to catch nested {% if ... %}{% endif %} statments with regex

This is what I got now: /{% if(.+?) %}(.*?){% endif %}/gusi It catches multiple if statements etc just fine. IMG: http://image.xesau.eu/2015-02-07_23-22-11.png But when I do nested ones, so an if in an if, it stops at the first occurence of {% endif…
Xesau
  • 161
  • 1
  • 9
-2
votes
2 answers

Regular expressions replace

I need to remove [0037][user name] combination from a sentence. In the first brackets always containing numbers eg: [0032] Digit count will not exceed than 4 by any chance. In the second brackets always containing letters eg: [first…
Lasith
  • 565
  • 1
  • 6
  • 17
-3
votes
1 answer

The /e modifier is deprecated, use preg_replace_callback instead - wordpress plugin

So i found this wordpress plugin, and it's the only one that offers what i need, but it's outdated :/ This is the error: Deprecated: preg_replace() [function.preg-replace]: The /e modifier is deprecated, use preg_replace_callback instead And…
-3
votes
1 answer

convert text url into clickable text or image link

Here's is something I would like to add to my website. I am proposing to have website users submit their profile page with links in this format: (*http://example.com X click_here*) (*https://www.facebook.com/xyz X facebook*) (*http://twitter.com…
Tom
  • 55
  • 2
  • 9
-4
votes
1 answer
1 2 3
27
28