Answer the question
In order to leave comments, you need to log in
How do you replace a temporary variable in regular expressions?
Here's what I want to do - so that after the preg_replace_callback has worked - there are key-value pairs in the $replacements variable.
In the local scope of the closure, the function works (inside $replacements contains everything you need). It is necessary to deduce outside - values vanish. I don’t know how to pass by reference correctly, it most likely works with a copy of the variable, and not with itself.
$total = '';
$pattern = "/<([a-z][a-z0-9_]+):([^:\r\n]+)>/";
$replacements = array();
$total = preg_replace_callback($pattern, function ($matches) use ($replacements) {
static $counter = 1;
$name = '@' . $counter;
$value = '(?P<' . $matches[1] . '>' . $matches[2] . ')';
$replacements[$name] =& $value; // ???????????????????
$counter++;
return $name;
}, $route);
print_r($replacements); // Array () - ???????????
exit();
Answer the question
In order to leave comments, you need to log in
Поспешил, людей насмешил. Самое главное то не проверил и оно как раз заработало:
preg_replace_callback($pattern, function ($matches) use (&$replacements)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question