ImpendingDoom ImpendingDoom - 11 months ago 36
PHP Question

How to parse a string using preg_replace_callback

I want to parse a string that looks something like this:


using preg_replace_callback():

// some regex to match anything that starts with '_' and ends with '_',
function($matches) {
// some operation

so anything that starts and ends with
is replaced, in the above example above it would be

more info:

  1. I need the right regex: something like

  2. The outcome would be a mathematical expression except the replaced values are calculated based on values from the DB, so
    is the identifier.

Answer Source

Give this a try:

echo preg_replace_callback(
    function($match) {
        $number = $match[1];
        return "[There used to be a $number here.]";
); # ((24x[There used to be a 12 here.])+([There used to be a 42 here.]+24))/2