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

Replace based on regex and return manipulated string

I have a logic query as a string, for example: mydate == 'one day ago' && mydate > 2014-05-16 16:00:00 I need every instance of a date replaced by a timestamp. My current code can replace the YYYY-MM-DD HH:MM:SS date-time to a timestamp using…
Craig
  • 431
  • 1
  • 4
  • 12
0
votes
1 answer

PHP preg_replace_callback catch more than one occurance in line

I search for custom HTML tags with: $match = preg_replace_callback("/(.*)/", function ($key) { $result = getTxt($key[1]); return $result; }, $buffer); It works when input is: Abc 1 efg But why it returns null on: Abc…
Szymon Toda
  • 4,454
  • 11
  • 43
  • 62
0
votes
3 answers

How to match something like shortcode that repeats and contains whatever including new lines

I am trying to process something similar like shrotcode via preg_replace_callback function: $new_content = preg_replace_callback("|\[BLOCK\s?(TYPE=[0-9a-z\/]+)?\s?(TEXT=[a-z]+)?\s?(LAST)?\s?\]((?:(?!BLOCK).)*)\[\/BLOCK\]|","block",$content); The…
0
votes
1 answer

preg_replace_callback returning same as input

$post = preg_replace_callback("/\[smartquote]((?:(?!\[smartquote\]).)+?)\[\/smartquote\]/s", function($match) use ($pdo){ if(isset($match[0])){ $returnData = explode('_',$match[1]); if(empty($returnData[0]) ||…
Jake
  • 1,469
  • 4
  • 19
  • 40
0
votes
2 answers

preg_replace_callback with Spoilers into Spoilers

i write something like [spoiler=Spoiler Title]text inside the Spoiler[/spoiler] and use preg_replace_callback("/\[spoiler=(.*)\](.*)\[\/spoiler\]/Usi", 'BBCode_spoiler', $text); to create a real spoiler, the result with one or more spoiler…
Alex Ruhl
  • 451
  • 2
  • 6
  • 16
0
votes
2 answers

PHP: add a linebreaks in a preg replace

I can understand most PHP code by just reading it, but I've never understood how preg_replace is used, so I've only been copying other peoples codes to get what I want. Now I need to add linebreaks in it, and I've tried multiple combinations but I…
saknar namn
  • 139
  • 1
  • 1
  • 10
0
votes
0 answers

preg_replace_callback returns empty

First of all, I should say that this is a WordPress based question, which I originally asked on the WordPress StackExchange, here. But I think it's turned into a more PHP-based question so that's why I'm asking here. So basically, I've written this…
Tom Oakley
  • 6,065
  • 11
  • 44
  • 73
0
votes
1 answer

Need to count matches and replace "nth" match using preg_replace_callback

I need to search through some content and enclose certain words with HTML. The words to search for are in an array, and I want to only replace the 2nd to last occurrence of each set of matches. So, as an example, if there are 5 matches, I want to…
gtr1971
  • 2,672
  • 2
  • 18
  • 23
0
votes
1 answer

php regex to match operators

I have the string 'OR "law studies"~2 AND here also NOT uni* "south West" word NOT this *eng' I want to use preg_replace_callback() to process all the process all words that are between (AND|OR|NOT) I'm struggling with the regex pattern. Can you…
gino4_0
  • 1
  • 1
0
votes
1 answer

Convert: "preg_replace" -> "preg_replace_callback"

I'm trying to update my code but I'm stuck at this codeline. How do I proceed to convert this to preg_replace_callback? $buffer = preg_replace("#§([a-z0-9-_]+)\.?([a-z0-9-_]+)?#ie","\$templ->\\1(\\2)",$buffer);
0
votes
1 answer

preg_replace_callback occurrences of match unless a backslash exists 0-20 chars ahead

For a long time I've been using preg_replace_callback to replace any occurrence of a reference ID into a link that opens the reference. For example, "BIZ1234" would be replaced with Name title (pseudo code) All good. But now I need to not do the…
mr_lou
  • 1,910
  • 2
  • 14
  • 26
0
votes
1 answer

preg_replace_callback function name with parameter in string

I am trying to use preg_replace_callback() to call any function with its parameter(s) embedded in a string. $string = "some text ucfirst('asd')"; $pattern = "~ucfirst([a-z]+)\(\)~"; $string = preg_replace_callback($pattern, "ucasef", $string); echo…
Stewart Megaw
  • 311
  • 2
  • 14
0
votes
1 answer

SugerCRM / SuiteCRM: Error with preg_replace() and modifier "e" in Smarty 2.6.0 on PHP 5.5.3

The PHP Template Engine: Smarty 2.6.0 (/includes/Smarty) is using a deprecated modifier 'e' for preg_replace function call: Block causing error: /include/Smarty/Smarty_Compiler.class.php @ line 262 /* replace special blocks by "{php}"…
0
votes
1 answer

Newest PHP and preg_replace()

Welcome. I have problem, becouse when I installed newest Xampp I have errors on my website, but I can not PHP and I do not know how to repair it. Error content: Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback…
0
votes
1 answer

How how do I replace the first and last instance in preg replace callback with PHP?

I am creating a custom forum in PHP and CodeIgniter. I am trying to code the bbCode parser, however, I am running into a bit of a problem. I use preg_replace_callback to replace the [quote id=123][/quote] tags. It works fine if there is only one…
ShoeLace1291
  • 4,551
  • 12
  • 45
  • 81