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
-1
votes
3 answers

How to get the matched substring in preg_replace?

I remove the first occurrence of a html tag in a string with preg_replace as $html = preg_replace('#

(.*?)

#i', '', $html, 1); How can I save the deleted substring as a new string: $deleted = '

...

';
Googlebot
  • 15,159
  • 44
  • 133
  • 229
-1
votes
2 answers

PHP - preg_replace_callback function

I have a preg_replace_callback function and when i open my webpage, i get the following warning: Warning: preg_replace_callback(): Requires argument 2, 'stripslashes(strstr("\2\5","rel=\class=") ? "\1" : This is my function: function…
Robert
  • 1
  • 1
-1
votes
2 answers

rendering template to real html using preg_replace

I want to replace template with HTML code, and it worked good, but the problem is that I use a php code inside the rendered HTML, and it can't run just printed like a string. this is the code: $template = '[text required id="first_name" label="First…
Mohamed Kamel
  • 93
  • 1
  • 13
-1
votes
1 answer

How do I stop parse errors and get code to work in PHP 5 AND 7

I have a server but it only has PHP 5 on it at the moment I have a broken arm so moving (just typing this) is a killer. I thought all the class_exists and function_exists functions built into PHP would at least be backward compatible however as PHP…
MonkeyMagix
  • 677
  • 2
  • 10
  • 30
-1
votes
1 answer

Replace preg_replace through preg_replace_callback

I have a PHP library here and I want to replace preg_replace with preg_replace_callback. This is the line: preg_replace("/=([0-9A-F][0-9A-F])/e", 'chr(hexdec("$1"))', $l) and this is what I did: preg_replace_callback("/=([0-9A-F][0-9A-F])/",…
wolfffi
  • 11
  • 1
-1
votes
1 answer

Need help to replace preg_replace_callback in php?

Need help to solve error like below, preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in line 601 Got error in below code, $string = preg_replace('~�*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))',…
-1
votes
1 answer

What does implode do in a pattern used for preg_replace_callback?

I am not a php dev and would like some help to understand $keys = array_keys($parameters['macrons']); $pattern = '/([' . implode('', $keys) . '])ー/u'; return preg_replace_callback($pattern, function($matches) use ($parameters) { …
user1464139
-1
votes
1 answer

Preg match callback not working

I'm trying to write a function that will replace any occurrence of numbers that start and ends with PID. I think the problem is in the regex but I can't figure it out. The code is below. I also tried just (.) instead of (.*) - no difference. I know…
user3052443
  • 758
  • 1
  • 7
  • 22
-1
votes
1 answer

PHP Replace the very first lower cased char of every line by upper case

I've tried using the below $string= 'hello world
this is newline

another line'; echo $string.'

'; echo preg_replace_callback('/^[a-zA-Z0-9]/m', function($m) { return strtoupper($m[0]); }, $string); the output: Hello world this is…
dany
  • 177
  • 2
  • 13
-1
votes
1 answer

PHP Regex not matching escaped values correctly

I have the following regex that matches correctly in https://regex101.com but not when using preg_replace_callback in PHP. (?
Kudit
  • 4,212
  • 2
  • 26
  • 32
-1
votes
1 answer

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback in Echelon B3

function removeColorCode($text) { return preg_replace('/\\^([0-9])/ie', '', $text); } The above code gives a deprecation warning on Echelon B3 i think after upgrading to PHP 5.5.29 by our host provider How can I replace the code…
Rodlake
  • 9
  • 4
-1
votes
1 answer

Preg replace callback

I am trying to work out how to pop an optional group into a preg_replace_callback but I am having trouble matching the + character Here is the code I have this far $pattern=preg_replace_callback('~:(?P<_param>[Aa-zZ0-9_\-]+)(?:(P<_mod>[^/]))?~', …
J.Doe
  • 21
  • 3
-1
votes
1 answer

note Warning: preg_replace_callback(): Requires argument

I board a small mistake I notice used to solve just by changing preg_replace preg_replace_callback put his put me another error that I board not understand if someone would have the privilege to explain the error I thank in advance. here is the…
roow
  • 7
  • 1
-1
votes
1 answer

How to replace the first occurrence only of any item in a list/array? (PHP)

I have a text like this: Paul loves Anna, Anna loves John, and John loves Betty. Unluckily, Betty loves Paul. Paul now hates Anna because she loves John. But John loves Betty, and that's why Anna is sad. Now Anna would like to marry Paul, but it's…
-1
votes
1 answer

preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead Array to string conversion

i use that function to convert URLs to active links public function make_links_blank($text) { return preg_replace( array( '/(?(?=]*>.+<\/a>) (?:]*>.+<\/a>) | …