I'm having a bit of trouble replacing parts of a string. I'm using preg_replace and it works but it replaces everything in between. I need it to search for every match between the string.

What I have so far:

$message = 'I have a [color]red[/color] apple and a [color]blue[/color] car.';
$pattern = '/(\[color])(.*)(\[\/color])/eis';
$message = preg_replace($pattern, "'<div class=\'color\'>'.'$2'.'</div>'", $message);

What it is giving me right now:

I have a <div class='color'>red[/color] apple and a [color]blue</div> car.

What I need it to say:

I have a <div class='color'>red</div> apple and a <div class='color'>blue</div> car.

Nevermind. I got it working. There was something wrong with the preg_replace.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.