Viewed   471 times
$result = preg_replace(
    "/{([<>])([a-zA-Z0-9_]*)(?{0,1})([a-zA-Z0-9_]*)}(.*){\1/\2}/iseU", 
    "CallFunction('\1','\2','\3','\4','\5')",
    $result
);

The above code gives a deprecation warning after upgrading to PHP 5.5:

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

How can I replace the code with preg_replace_callback()?

 Answers

1

You can use an anonymous function to pass the matches to your function:

$result = preg_replace_callback(
    "/{([<>])([a-zA-Z0-9_]*)(?{0,1})([a-zA-Z0-9_]*)}(.*){\1/\2}/isU",
    function($m) { return CallFunction($m[1], $m[2], $m[3], $m[4], $m[5]); },
    $result
);

Apart from being faster, this will also properly handle double quotes in your string. Your current code using /e would convert a double quote " into ".

Saturday, December 17, 2022
2

the entire ereg family of functions are deprecated in PHP and will at some point be removed from the language. The replacement is the preg family. For the most part, the change is simple:

preg_replace('/[^<>]>/i', '', $question);
^--           ^      ^^
  1. change ereg to preg
  2. add delimeters (/)
  3. for case insensitive matches (eregi), add the i modifier
Friday, August 5, 2022
5

I have created a demonstration to show how to call getOpenGraph() and how the capture groups are passed as arguments without specifying them in the second parameter of preg_replace_callback().

I modified the pattern delimiters so that the slash in the end tag doesn't need to be escaped.

function getOpenGraph($matches){
    return strrev($matches[1]);  // just reverse the string for effect
}

$input='Leading text [ourl]This is ourl-wrapped text[/ourl] trailing text';
$pattern='~[ourl](.*?)[/ourl]~i';
$output=preg_replace_callback($pattern,'getOpenGraph',$input);   
echo $output;

Output:

Leading text txet depparw-lruo si sihT trailing text
Saturday, December 24, 2022
 
4

Use the following:

$out = preg_replace_callback(
    "([otsection](.*?)[/otsection])is",
    function($m) {
        static $id = 0;
        $id++;
        return "<div class="otsection" id="ots".$id."">".$m[1]."</div>";
    },
    $in);

In particular, note that I used a static variable. This variable persists across calls to the function, meaning that it will be incremented every time the function is called, which happens for each match.

Also, note that I prepended ots to the ID. Element IDs should not start with numbers.


For PHP before 5.3:

$out = preg_replace_callback(
    "([otsection](.*?)[/otsection])is",
    create_function('$m','
        static $id = 0;
        $id++;
        return "<div class="otsection" id="ots".$id."">".$m[1]."</div>";
    '),
    $in);
Monday, September 5, 2022
 
1

You can continue with this and this will work. Deprecation means that there is new recommended alternative, but off course old code will still work. The new way of doing that would be using FastFeatureDetector or AgastFeatureDetector depending on your use case. I am not familiar with OpenCV so unfortunately I can't recommend which exact implementation you need, you need to read JavaDoc/other docs and find out which would fit your code.

Thursday, August 25, 2022
 
akbari
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :