1

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 ?

8
  • 1
    Your code has a few mistakes - the inner quotes in "<span class="c">{chord}</span>" need to be escaped, new_String doesn't have a $ and probably should be $string
    – Nigel Ren
    Commented Jul 16, 2019 at 17:19
  • yes you are right but I just use simple example for my problem . Edited
    – Pablo
    Commented Jul 16, 2019 at 17:20
  • With the corrections - your example works and gives <span class="c">{chord}</span><span class="c">{chord}</span><span class="c">{chord}</span><span class="c">{chord}</span>
    – Nigel Ren
    Commented Jul 16, 2019 at 17:21
  • @KIKOSoftware, ermmm - don't tell anyone as it's a trade secret - but I ran the code and printed out the result.
    – Nigel Ren
    Commented Jul 16, 2019 at 17:23
  • 1
    Use $arr = array_unique($arr); and then do what you are doing. Commented Jul 16, 2019 at 17:34

2 Answers 2

1

You are using preg_replace a wrong way: the main interest of this function is to take a regex pattern as parameter, and a pattern isn't to describe a fixed string but can describe several kind of strings (and this way you don't have to use a foreach loop since you can replace the whole string in 1 pass), example:

$result = preg_replace('/{[A-G]}/', '<span class="c">$0</span>', $string);

Other way, since you want to replace only fixed strings, you can use strtr that also does the job in one pass:

$arr = ["{A}","{B}","{C}"];
$rep = array_map(function($i) { return '<span class="c">' . $i . '</span>'; }, $arr);
$trans = array_combine($arr, $rep);

$result = strtr($string, $trans);
1
  • OP isn't upping, so I am. Good answer. Commented Jul 21, 2019 at 10:22
0

In addition to Casimir's suggested snippets, you can also use this one:

Code: (Demo)

$arr   = ["{A}", "{B}", "{C}", "{A}"];
$start = '<span class="c">';
$end   = '</span>';
$string = "{A}{B}{C}{A}";

foreach (array_unique($arr) as $item) {
    $string = str_replace($item, $start . $item . $end, $string);
}

echo $string;

It alters how you define the replacement, but avoids regex.

As a more robust, solution (if your project requires it), using strtr() is ideal for multiple replacements on the same string because it avoid replacing a replacement. As you can see in Casimir's answer, there's a bit of preparation required.

Here's a language-construct version of the Casimir's technique: (Demo)

$arr   = ["{A}", "{B}", "{C}", "{A}"];

foreach (array_unique($arr) as $item) {
    $translator[$item] = '<span class="c">' . $item . '</span>';
}

var_export($translator);
echo "\n\n---\n\n";

$string = "{A}{B}{C}{A}";

echo strtr($string, $translator);

Output:

array (
  '{A}' => '<span class="c">{A}</span>',
  '{B}' => '<span class="c">{B}</span>',
  '{C}' => '<span class="c">{C}</span>',
)

---

<span class="c">{A}</span><span class="c">{B}</span><span class="c">{C}</span><span class="c">{A}</span>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.