I have this code and my question is how can I use preg_replace to replace string one by one
$arr = ["{A}","{B}","{C}","{A}"];
$string = "{A}{B}{C}{A}";
foreach ($arr as $item){
$replacement = "<span class=\"c\">{$item}</span>";
$new_String = preg_replace("/$item/",$replacement ,$string);
}
the result is this :
<span class="c">
<span class="c">{A}</span>
</span>
<span class="c">{B}</span>
<span class="c">{C}</span>
<span class="c">
<span class="c">{A}</span>
</span>
Because I have 2 {A} in my string preg_replace make 2 span for both of the {A} . how to fix this ?
"<span class="c">{chord}</span>"
need to be escaped,new_String
doesn't have a$
and probably should be$string
<span class="c">{chord}</span><span class="c">{chord}</span><span class="c">{chord}</span><span class="c">{chord}</span>
$arr = array_unique($arr);
and then do what you are doing.