0

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.

Edited by mike_2000_17: Fixed formatting

1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by MagicMedia
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.