preg_replace and a function?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
elecktricity
Forum Contributor
Posts: 128
Joined: Sun Sep 25, 2005 8:57 pm
Location: Trapped in my own little world.
Contact:

preg_replace and a function?

Post 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?
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post 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);
}
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Post Reply