Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
hi all,

     i found a cool way for templates:
print preg_replace('/\b(\w+)\b/',doIt("$1"),"one two";

replaces the matching string with the return-value of
a selfdefined function. the problem is: the argument
of the function is passed as a reference to $1, the
matching string. is there a possibility to chance
the $1 into the matching string in runtime? tia,


Re: preg_replace-problem

*** Michael Wronna escribió/wrote (Sat, 9 Feb 2008 10:32:19 +0100):
Quoted text here. Click to load it

You have two alternatives:

- preg_replace_callback() lets you provide a custom funcion.
- The /e modifier in the regexp lets you use PHP in the replacement string

// Examples are NOT TESTED:
function doIt($matches){
    return 'foo' . $matches[1]  . 'bar';
print preg_replace_callback('/\b(\w+)\b/', 'doIt',"one two");

function doIt($match){
    return 'foo' . $match . 'bar';
print preg_replace('/\b(\w+)\b/e','doIt("$1")',"one two");

-+ - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web:
+- Mi web de humor austrohúngaro:

Site Timeline