A
A
artmirarmi2018-09-23 16:47:10
PHP
artmirarmi, 2018-09-23 16:47:10

How to do regular expression substitution?

Hello.
I need to do a regular expression replacement, I do it like this:

preg_replace('/{{(.+)}}/', substr('$1', 0, -5), $text);

The second parameter replacement can contain references of the form $n (any number instead of n). Each such link will be replaced with a substring corresponding to the nth subpattern.
But the problem is that when calling the substr(); this link is not substituted into the first parameter, but I need to do it. That is, I need a substring corresponding to this mask (.+) to be passed to the substr function as the first parameter.
Tell me how can I do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Hog, 2018-09-23
@artmirarmi

$text = preg_replace_callback('/{{(.+)}}/', function ($value) {
    return substr($value[1], 0, -5);
}, $text);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question