I'm building a hand-made function in php that searches for specific tags ( [b][/b] for bold, [i][/i] for italic and [img][/img] for pictures ) in a string, to replace them into their html equivalent. I also have to treat what's between [img] [/img] tags (can be more than one in a string) in a seperate function before the replacement, i've called it foo here :
<?php
function convert($txt){
$patterns = array( '/\[b\](.*?)\[\/b\]/' ,
'/\[i\](.*?)\[\/i\]/' ,
'/\[img\](.*?)\[\/img\]/');
$replace = array("<b>$1</b>" , "<i>$1</i>" , foo("$1") );
$res = preg_replace($patterns,$replace, $txt);
return $res;
}
It works correctly for the b and i tags, but not img.
The issue here is that : When i put the captured group (referenced by "$1" i think) in a function, it treats "$1" as a string, and not what is referenced by it. For example, if foo is declared like so :
function foo($var){
echo $var;
}
If i put the string text1 [img]path[/img] text2
in convert()
Then "$1"
will be echoed, instead of "path"
Therefore here is my question : How can i "evaluate" the string i've captured in a different function. In the previous example, to echo in foo what is between [img][/img] tags?
Thank you for anyone taking time to reply.