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

Update variable outside of preg_replace_callback from within the callback

I have a function which looks something like this... function filterwords($input='poo poo hello world bum trump'){ $report_swear = 0; $badwords = array('poo','bum','trump'); $filterCount = sizeof($badwords); for($i=0;…
Tom
  • 12,776
  • 48
  • 145
  • 240
2
votes
1 answer

preg_replace this: {{1}{2}{3}{..}} -- Likely a simple PHP / preg_replace / RegEx issue but I am honestly stumped

Working on a personal project I am using PHP and I'd like to run a preg_replace_callback function against the following strings: 1. {{hello}} 2. {{hello}{there}{how}{are}{you}} I'd like to detect the hello there how are and you and send to a…
mrmrw
  • 120
  • 9
2
votes
2 answers

regex for PHP preg_replace_callback

Regular expressions is just not my thing. :( I have a string that may contain multiple sub strings such as: [var1="111" var2="222" var3="222" var4="444"] I basically need to replace each occurrence with the results of a function that gets each…
Alex
  • 1,535
  • 2
  • 16
  • 26
2
votes
1 answer

FROM preg_replace TO preg_replace_callback

Hello fellow netheads! I'm having issues with updating some old function to preg_replace_callback. Edit: what am I doing wrong? This is my first (preg_replace/deprecated) function: if ($handle) { while (!feof($handle)) { $buffer = fgets($handle,…
mlv
  • 55
  • 2
  • 7
2
votes
1 answer

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

I need a little help. Because preg_replace is deprecated, I have to convert all my preg_replace to preg_replace_callback... function parse_bb_tpl ($part, $args) { // just a replace, with evaluation... return preg_replace ( …
2
votes
2 answers

preg_replace_callback calling $this->method('\\1') at parameter 2

erm...i have a class, trying to make a template class, using preg_replace_callback, but i don't know how the parameter 2 write class template { public function parse_template($newtpl, $cachetpl){ ...... $template =…
user259752
  • 1,065
  • 2
  • 9
  • 24
2
votes
1 answer

Using preg_replace_callback with shortcodes

I'm trying to implement short codes on my website to make publishing easier. What i got so far look like this: $text = "[button]Hello[button] [input]holololo[input]"; $shortcodes = Array( 'button' => '', 'input' => '
Murillio4
  • 485
  • 5
  • 17
2
votes
1 answer

How do I pass a regex parameter to a preg replace callback in PHP?

I am using preg_replace_callback to parse my [quote id=123][/quote] bulletin board code tag. What is the correct way to pass the quote id with a regex parameter? $str = '[quote id=123]This is a quote tag[/quote]'; $str =…
ShoeLace1291
  • 4,551
  • 12
  • 45
  • 81
2
votes
1 answer

Using text styling to create a list with sublists

I am trying to make markup to format an Ordered list, here is the markup style: $strings = "1. dog 1. cat 1. fish 1. horse 1. monkey 1. pig "; horse and monkey from that list should be part of a sublist, since they have one space before the…
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
2
votes
1 answer

How to convert preg_replace e to preg_replace_callback?

Okay, so I'm slightly confused. Here is the code I have now, but I just found out the e modifier is deprecated. How do I convert it to a preg_replace_callback()? I still haven't figured it out. $post = preg_replace("/\[code\]([^] )\[\/code\]/e",…
Jake
  • 1,469
  • 4
  • 19
  • 40
2
votes
1 answer

Use a local variable in preg_replace_callback - PHP

How to use a local variable in preg_replace_callback in PHP. I have the following code: function pregRep($matches) { global $i; $i++; if($i > 2) { return '#'.$matches[0]; } else { return $matches[0]; …
sanchitkhanna26
  • 2,143
  • 8
  • 28
  • 42
2
votes
1 answer

regex error preg_replace_callback() : Unknown modifier '.' in

I got the following string: last_name, first_name bjorge, philip kardashian, [kghim] mer#$##Code:menu:51587daa7030e##$#cury some more data #$##Code:menu:515r4387daa7dsf030e##$#, freddie im trying to replace the Codes in the middle with the…
Mike
  • 741
  • 13
  • 40
2
votes
2 answers

PHP regex pattern for using preg_replace_callback to eval html's if/else condition

I want to have a regex pattern to evaluate the if/else in manually-created html tags like this: Return value if true Return value if false And, more specifically:
Tsuki
  • 33
  • 5
2
votes
2 answers

PHP preg_replace_callback() and create_function() with eval() do not work

I'm trying to deploy my website on the Internet in order to test it in the real environment. It's some kind of text editor where users can use regular expressions and user-defined callback functions. I have some problem with preg_replace_callback()…
Placido
  • 1,386
  • 1
  • 12
  • 24
2
votes
3 answers

Does python's re.sub take an array as the input parameter?

According to http://www.php2python.com/wiki/function.preg-replace-callback/ re.sub is the python equivlant of PHP's preg_replace_callback, but the php version takes an array for the strings to be matched, so you can pass multiple strings, but the…
vfclists
  • 19,193
  • 21
  • 73
  • 92