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
0
votes
2 answers

preg_replace_callback Matching URLs in HTML Paragraphs

I am trying to take the URLs that are in single HTML paragraphs and extract them with PHP's preg_replace_callback. Right now, WordPress does this with: preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', 'callback_function', $string ); But…
Sean Fisher
  • 625
  • 5
  • 13
0
votes
3 answers

PHP: run function at certain places (preg_callback?)

I'm trying to run a function whenever there's a [%xxx%] (acting as a placeholder, if you will), e.g.: Bla bla bla blabla. Blablabla bla bla. [%hooray%] Blabla hey bla bla. [%yay%] blabla bla. I'm pretty much a PHP beginner, but I managed to crack…
AKG
  • 2,936
  • 5
  • 27
  • 36
0
votes
1 answer

PHP preg_replace_callback works on 5.3 but not on 5.2.17

i have the following code: return preg_replace_callback("#\{gallery: '(.+?)'(?: dir: ([0-1]))?\}#i", create_function('$i', '$dir = isset( $i[2] ) ? 1 : 0; $oGallery = new Gallery( $i[1] ) ; $oGallery->PublicSide($dir);' ), $string); the problem…
Waldir Bolanos
  • 422
  • 3
  • 7
0
votes
1 answer

PHP preg_replace_callback

I have this tag into my html file: {{block:my_test_block}} {{news:my_test_block2}} I need to parse and replace this tag with content from db, my aproach: ob_start(); require_once('file.php'); $template = ob_get_contents(); …
Ste
  • 1,497
  • 8
  • 33
  • 63
0
votes
5 answers

PHP: How to make variable visible in create_function()?

This code: $t = 100; $str = preg_replace_callback("/(Name[A-Z]+[0-9]*)/", create_function( '$matches', 'return $matches[1] + $t;' …
0
votes
2 answers

RegEx: How to extract a value/number from a pattern and replace it with something else?

For instance I have this piece of data: array( 1 => 'Metallica', 2 => 'Megadeth', 3 => 'Anthrax', 4 => 'Slayer', 5 => 'Black Sabbath', ); And I have this piece of text: My first favorite band is: #band{2}, and after that it's:…
user798596
-1
votes
0 answers

I need advise with PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead

I'm fixing an old script, that is thowing that warning (PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead) in this line: '.preg_replace("|\[session id=\"(.*)\"\](.*)\[/session\]|e",…
darthcharmander
  • 129
  • 1
  • 7
-1
votes
2 answers

How to replace below preg_replace with preg_replace_callback?

I am having difficulty converting below preg_replace() function call preg_replace("/\{(.*?)\}/e", '$\1', $data) to using preg_replace_callback() (because of the removed e modifier in PHP 7.0). I have tried this but I have no idea how to fully…
user2736878
  • 31
  • 1
  • 8
-1
votes
1 answer

Replace only quotes of a specific tag in a string php

In a string like this one Améliorer mon authentification i'm trying to replace this style="color:black;" with style= \ "color:black; \ " i'm trying to use preg_replace like that $result =…
-1
votes
1 answer

How to write a valid callback for preg_replace_callback(): Requires argument 2, ''s:'.strlen('$2').':"$2";'',

In my config.php I have this old PHP5 function that I have tried to update for use in PHP7, but this error and warning is spoiling things: PHP Warning: preg_replace_callback(): Requires argument 2, ''s:'.strlen('$2').':"$2";'', to be a valid…
Wes
  • 1
-1
votes
2 answers

how to use str replace in php

Array ( [0] => Array ( [trigger] => E [alttext] => C ) [1] => Array ( [trigger] => F [alttext] =>D ) ) foreach ($trigger as $tdata) { $tr = $tdata['trigger']; $ttext = $tdata['alttext']; $text=…
Shuvo
  • 17
  • 5
-1
votes
1 answer

using preg_replace_callback for multiple matches

I tried a simple regex using preg_replace_callback as $str = 'Key Value'; $str = preg_replace_callback('/(key) (.*?)/', function($m) { return $m[2].$m[1]; }, $str); echo $str; I expected the output would be ValueKey but it doesn't. Where did I…
Googlebot
  • 15,159
  • 44
  • 133
  • 229
-1
votes
2 answers

PHP preg_replace_callback match string but exclude urls

What I'm trying to do is find all the matches within a content block, but ignore anything that is inside tags, for use inside preg_replace_callback(). For example: test test title test In this case, I want the first line to…
Ben
  • 1
  • 2
-1
votes
1 answer

Regex with preg_replace_callback used for profanity filter

I had a working function that was getting an array of bad words then replacing the bad words with asterisks. When I upgraded to PHP7 I had to use preg_replace_callback since the preg_replace e modifier was depreciated. This is how I was using it:…
626
  • 1,159
  • 2
  • 16
  • 27
-1
votes
1 answer

stuck with Preg_replace() to Preg_replace_callback()

I am trying to convert this function to preg_replace_callback but almost everything that I tried gives error: Requires argument 2, '$db->module', to be a valid callback in This is my code: $this->template = preg_replace ("/#module\=(\w+)#/ie",…
Dikobraz
  • 649
  • 2
  • 8
  • 22