0

I have this regex which can extract any substrings like {{string}} my regex: '/{{+(.*?)}}/'

and I need to modify it where it could extract strings like {{ something -enter-char-\r-OR\r\n- somthing }}

function replace_callback($matches)
{
    // just in case
    if ( !isset($matches[1]) || !is_string($matches[1]) ) throw new Exception(__FUNCTION__.': invalid $matches[1].');
    // detected var without brackets
    $var = $matches[1];

    // my program's processing on $var

    return $var;
}

$text = ' sdfhh fdfdf {{do:somecoolthings}} sdfggg';

echo preg_replace_callback(
    '/{{+(.*?)}}/',
    'replace_callback',
    $text
);

thanks in advance!

Edit: The people who see questions like this are programmers who don't have as much knowledge about regex, and this question asks a different goal than the other 2 "duplicate" questions, and I'm sure this post will become useful to someone.

final code:

function replace_callback($matches)
{
    // just in case
    if ( !isset($matches[1]) || !is_string($matches[1]) ) throw new Exception(__FUNCTION__.': invalid $matches[1].');
    // detected var without brackets
    $var = $matches[1];

    // my program's processing on $var

    return $var;
}

$text = ' sdfhh fdfdf {{
do:somecoolthings
}} sdfggg';

echo preg_replace_callback(
    '/{{(.*?)}}/s',
    'replace_callback',
    $text
);
Mac A.
  • 103
  • 8

0 Answers0