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
404 questions
84
votes
3 answers

Replace preg_replace() e modifier with preg_replace_callback

I'm terrible with regular expressions. I'm trying to replace this: public static function camelize($word) { return preg_replace('/(^|_)([a-z])/e', 'strtoupper("\\2")', $word); } with preg_replace_callback with an anonymous function. I don't…
Casey
  • 1,841
  • 3
  • 15
  • 29
48
votes
1 answer

Replace deprecated preg_replace /e with preg_replace_callback

$result = preg_replace( "/\{([<>])([a-zA-Z0-9_]*)(\?{0,1})([a-zA-Z0-9_]*)\}(.*)\{\\1\/\\2\}/iseU", "CallFunction('\\1','\\2','\\3','\\4','\\5')", $result ); The above code gives a deprecation warning after upgrading to PHP…
dima.h
  • 850
  • 2
  • 10
  • 14
41
votes
2 answers

How to use preg_replace_callback?

I have the following HTML statement [otsection]Wallpapers[/otsection] WALLPAPERS GO HERE [otsection]Videos[/otsection] VIDEOS GO HERE What I am trying to do is replace the [otsection] tags with an html div. The catch is I want to increment the id…
Mark
  • 2,701
  • 9
  • 23
  • 41
18
votes
7 answers

Symfony 1.4 using deprecated functions in php 5.5

I recently upgraded PHP from version 5.3.27 to 5.5.0. Everything is working fine in my Symfony 2.3.2 project, and I can enjoy the latest PHP functionalities. Now when I am going back to my other Symfony 1.4.16 project, I get a PHP error about…
mika
  • 1,911
  • 3
  • 17
  • 31
14
votes
1 answer

How do I access a variable inside of preg_replace_callback?

I'm trying to replace {{key}} items in my $text with values from a passed array. but when I tried adding the print_r to see what was going on I got a Undefined variable: kvPairs error. How can I access my variable form within the…
Justin808
  • 19,126
  • 41
  • 143
  • 241
12
votes
6 answers

How can I code for multiple versions of PHP in the same file without error?

I'm trying to support two versions of some PHP code in one file using version_compare, but I still get an error. Code: if (version_compare(PHP_VERSION, '5.3.0') >= 0) { $alias = preg_replace_callback('/&#x([0-9a-f]{1,7});/i', function($matches)…
orbitory
  • 1,002
  • 5
  • 14
  • 35
9
votes
3 answers

Is there a way to pass another parameter in the preg_replace_callback callback function?

mmmh guys, i really hope my english is good enaught to explain what i need. Lets take this example (that is just an example!) of code: class Something(){ public function Lower($string){ return strtolower($string); } } class Foo{ …
Strae
  • 17,313
  • 26
  • 89
  • 129
5
votes
1 answer

Dynamically Replacing by a Function in PHP

Is there a way to dynamically replace by a function, similar to how .replace works in JavaScript? A comparable use case would be I have a string with numbers and I would like to pass the numbers through a function: "1 2 3" => "2 4 6" (x2) The…
Derek 朕會功夫
  • 84,678
  • 41
  • 166
  • 228
4
votes
2 answers

Find text inside curly braces and replace text including curly braces

I would like to find a pattern {text} and replace text including curly braces. $data = 'you will have a {text and text} in such a format to do {code and code}'; $data= preg_replace_callback('/(?<={{)[^}]*(?=}})/', array($this,…
Shadow
  • 103
  • 1
  • 6
4
votes
2 answers

How to recognize tokens between repeated delimiters?

I am trying to parse templates where tokens are delimited by @ on both sides. Example input: Hello, @name@! Please contact admin@example.com, dear @name@! Desired output: Hello, Peter! Please contact admin@example.com, dear Peter! Naive attempt…
Džuris
  • 1,729
  • 3
  • 22
  • 41
4
votes
1 answer

PHP7 - The /e modifier is no longer supported, use preg_replace_callback instead

Can somebody help me with this error I'm getting? Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead My original code: $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")',…
kironet
  • 595
  • 1
  • 7
  • 24
4
votes
1 answer

How to convert foreign characters in regex search to proper case?

I have written the function below. It converts lower caser to upper case and proper case. I want it to ignore foreign characters. eg. ñ Expected result: Sabiña/Cerca Actual Result: SabiÑA/Cerca NOTE: if I use mb_convert_case alone it does not change…
user2635901
  • 183
  • 1
  • 12
4
votes
1 answer

php converting preg_replace to preg_replace_callback

I'm working on this old code, and ran across this - which fails: preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $sObject); It tells me that preg_replace e modifier is deprecated, and to use preg_replace_callback instead. From…
4
votes
2 answers

Replace preg_replace() to preg_replace_callback()

$source = preg_replace('/&#(\d+);/me', "utf8_encode(chr(\\1))", $source); The above code gives deprecated warning. Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in How can I replace preg_replace()…
memmedimanli
  • 77
  • 1
  • 1
  • 9
4
votes
1 answer

Find and replace URLs in a blob of text but exclude those in link tags

I have been trying to run through a string and find and replace URLs with a link, here what I have come up so far, and it does seem to work for the most part quite well, however there are a few things I'd like to polish. Also it might not be the…
Czechmate
  • 41
  • 2
1
2 3
26 27