preg_replace pass match through function before replacing

Posted by Martin on Stack Overflow See other posts from Stack Overflow or by Martin
Published on 2010-03-31T14:49:24Z Indexed on 2010/03/31 14:53 UTC
Read the original article Hit count: 286

Filed under:
|
|

This is what i want to do:

$line = 'blabla translate("test") blabla';
$line = preg_replace("/(.*?)translate\((.*?)\)(.*?)/","$1".translate("$2")."$3",$line);

So the result should be that translate("test") is replaced with the translation of "test".

The problem is that translate("$2") passes the string "$2" to the translate function. So translate() tries to translate "$2" instead of "test".

Is there some way to pass the value of the match to a function before replacing?

© Stack Overflow or respective owner

Related posts about php

Related posts about preg-replace