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

Using preg_replace_callback() to extract all images from a string of HTML

Tricky preg_replace_callback function here - I am admittedly not great at PRCE expressions. I am trying to extract all img src values from a string of HTML, save the img src values to an array, and additionally replace the img src path to a local…
hotstuff
  • 21
  • 2
2
votes
1 answer

Collapsable print_r() tree with PHP 7 (w/o preg_replace() and /e)

In order to print_r a collapsable tree, I'm currently using his code which uses preg_replace() and the /e modifier, which is depreciated in PHP7:
2
votes
1 answer

use preg_replace_callback with array

I know this has been asked elsewhere, but I can't find the precise situation (and understand it!) so I'm hoping someone might be able to help with code here. There's an array of changes to be made. Simplified it's: $title = "Tom's wife is called…
arathra
  • 155
  • 1
  • 11
2
votes
2 answers

PHP Match All Character in New Line with Regex

I want to match any character after [nextpage] bbcode but the below bbcode does not match texts that follow when i make a break line. $string="[nextpage] This is how i decided to make a living with my laptop. This doesn't prevent me from doing some…
Omotayo
  • 59
  • 6
2
votes
0 answers

How correct incorrect words from dictionary array?

I've dictionary array with correct words: I tried some options for correction, but it did not help me to solve my problem. I will give an example of one of the methods…
Andreas Hunter
  • 4,504
  • 11
  • 65
  • 125
2
votes
3 answers

Geshi with Markdown

Im trying to get GeSHi to work with markdown. A simple use for Geshi is as follows: $geshi = new GeSHi($message, 'c'); print $geshi->parse_code(); The above code takes in the whole of message and turns it into Highlighted code I also have my…
Angel.King.47
  • 7,922
  • 14
  • 60
  • 85
2
votes
1 answer

preg_replace, preg_replace_callback and Array to string conversion

I have this code from an app in PHP 5.4 : $rightKey = preg_replace(array( "/(_)(\p{L}{1})/eu", "/(^\p{Ll}{1})/eu" ), array( "mb_strtoupper('\\2', 'UTF-8')", …
2
votes
3 answers

Preg replace image src using callback

I have an article with HTML tags. It's a long article with 3/5 images. Now I want to update every image src attributes. Example: Image html tag looks like:
2
votes
1 answer

PHP regular expressions - Replacing one backreference

Is there a way in preg_replace() or preg_replace_callback() to replace only one backreference? For instance I have this regular expression: /(\.popup)([\s\.{])/ and this string .popup .popup-option. The backreferences that will be generated are as…
André Silva
  • 1,110
  • 8
  • 24
2
votes
1 answer

need to change preg_repalce for php5.6

I have problem to change preg_replace to preg_replace_callback in the following piece of code in my PHP program. function category_get_tree($prefix = '', $tpl = '{name}', $no_prefix = true, $id = 0, $level = 0){ global $sql, $PHP_SELF; static…
Waterfall
  • 21
  • 2
2
votes
1 answer

Allow write access to array in PHP preg_replace_callback

I'm trying to write to an array which was initialized outside an anonymous preg_replace_callback function. I've tried the "use" keyword, and also declaring the variable "global", but neither seem to work. Here is my…
praine
  • 405
  • 1
  • 3
  • 14
2
votes
1 answer

convert preg_replace to preg_replace_callback modifier error

i have this code: $key = preg_replace( '/(^|[a-z])([A-Z])/e', 'strtolower(strlen("\\1") ? "\\1_\\2" : "\\2")', substr($method, 3) ); I receive php warning ( php 5.6 ), and i try to convert it with…
Ste
  • 1,497
  • 8
  • 33
  • 63
2
votes
1 answer

Replacing preg_replace by preg_replace_callback is giving me a warning

This is my code. private function _checkMatch($modFilePath, $checkFilePath) { $modFilePath = str_replace('\\', '/', $modFilePath); $checkFilePath = str_replace('\\', '/', $checkFilePath); $modFilePath =…
Ali Zia
  • 3,825
  • 5
  • 29
  • 77
2
votes
1 answer

Substitute reg_replace with reg_replace_callback in php

I am rather new to OpenCart 2.1 and as part of an attempt to install new theme i have encountered what seems to be an issue migrating to PHP 5.5. The error that i am getting is: Unknown: preg_replace(): The /e modifier is deprecated, use …
Sky21.86
  • 627
  • 2
  • 9
  • 26
2
votes
1 answer

Can't convert preg_replace() with modifier /e to preg_replace_callback()

I am using preg_replace() to replace {#page} with the actual value of the variable $page. Of course I have a lot of {#variable}, not just {#page}. For example: $uri = "module/page/{#page}"; $page = 3; //preg_replace that its working…
ntan
  • 2,195
  • 7
  • 35
  • 56