0

How do I use the values "returned" by preg_replace like in the following?

preg_replace($pattern, function('$1','$2'), $contents);

So it will pass the matches instead of $1 and $2 as strings ('$1' and '$2');

2 Answers 2

2

If function('$1','$2') really is a function call, then you cannot do it like that. It would be called before preg_replace is executed. In this case, you can use preg_replace_callback:

function foo($matches) {
    // $matches[1] is $1
    // $matches[2] is $2
}

preg_replace_callback($pattern, 'foo', $contents);

If it is not a function call, you have to explain better what you want to do.

Sign up to request clarification or add additional context in comments.

Comments

1
$pattern = '/blabla/e'; //make sure to use the 'e' modifier
$result = preg_replace($pattern, "function('\\1', '\\2')", $contents);

1 Comment

A side note: do not use the e-modifier unless you absolutely have to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.