I have a question about preg_replace() function. I'm using it with 'e' modifier. Here is code snippet:
$batchId = 2345;
$code = preg_replace("/[A-Za-z]{2,4}[\d\_]{1,5}[\.YRCc]{0,4}[\#\&\@\^]{0,2}/e",
'translate_indicator(\'$0\', {$batchId})', $code);
I want to have access to $batchId
variable inside translate_indicator($code, $batch=false)
function. The above exapmle, unfortunately, doesn't work correctly: $batch is invisible(var_dump()
result is bool(false)
) within translate_indicator()
.
Probably, I have syntax mistakes in replacement code. Or, maybe, it's impossible to pass variables with preg_replace()
?
Update for the first two answers.
Thank you for answers, but your advice didn't help. Besides I've already tried double qoutes instead of single qoutes. I've just simplified code to test possibility of passing parameter to the function:
$code = preg_replace("/[A-Za-z]{2,4}[\d\_]{1,5}[\.YRCc]{0,4}[\#\&\@\^]{0,2}/e",
"translate_indicator('$0', 12)", $code);
Also I've removed default value for the $batch within translate_indicator()
. Result:
Warning: Missing argument 2 for translate_indicator()
So I think it's impossible to pass parameter using this approach.:(