Page 1 of 1

preg_replace and a function?

Posted: Thu May 25, 2006 12:36 pm
by elecktricity

Code: Select all

$str = 'hello [i=55] this work?';
$str = preg_replace ('/\[i=(.*?)\]/is', workstuff($1), $str);
workstuff is a function I need $1 to go in but it wont work, probally isnt supported, there another way of going about this?

Posted: Thu May 25, 2006 12:47 pm
by TheMoose
You have to find all the matches first and work off each one in a loop, such as:

Code: Select all

$str = 'hello [i=55] this work?';
preg_match_all('/\[i=(.*?)\]/is', $str, $matches);
foreach ($matches[1] as $find) {
	$str = preg_replace ('/\[i=' . $find . '\]/is', workstuff($find), $str);
}

Posted: Thu May 25, 2006 1:30 pm
by timvw