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
87
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,941
  • 3
  • 17
  • 30
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
  • 860
  • 2
  • 10
  • 14
42
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
  • 3,653
  • 10
  • 30
  • 62
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,971
  • 3
  • 18
  • 32
16
votes
2 answers

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
  • 20,859
  • 46
  • 160
  • 265
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,090
  • 5
  • 16
  • 40
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
  • 18,807
  • 29
  • 92
  • 131
8
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
  • 766
  • 2
  • 11
  • 27
5
votes
3 answers

Access the offset of the current match in the callback function of preg_replace_callback()

How can I keep track of the current match’s offset from the start of the string in the callback of preg_replace_callback? For example, in this code, I’d like to point to the location of the match that throws the exception: $substituted =…
Ry-
  • 218,210
  • 55
  • 464
  • 476
5
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
  • 141
  • 1
  • 7
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 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
5
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…
5
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
  • 87
  • 1
  • 1
  • 9
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
  • 2,115
  • 3
  • 27
  • 55
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
  • 203
  • 2
  • 12
1
2 3
27 28