I had the following PHP Regex code, to replace all emails with a "[removed email]" replacement string; and it worked nicely:
$pattern = "/[^@\s]*@[^@\s]*\.[^@\s]*/";
$replacement = "[removed email]";
$body_highlighted = preg_replace($pattern, $replacement, $body_highlighted);
However, the email replacement strategy changed and now I need to actually show the emails but replace certain parts of it. I wanted to use str_replace on the numeric backreferences, like this, but it doesn't work.
$pattern = "/[^@\s]*@[^@\s]*\.[^@\s]*/";
$email_part = "$0";
$replacement = str_replace('a','b', $email_part); // replace all letter A with B in each email
$body_highlighted = preg_replace($pattern, $replacement, $body_highlighted);
Any idea what I am doing wrong?
str_replaceruns at calling, not as a binding. There is noain$0so it remains unchanged. Likely you want to be usingpreg_replace_callback.