Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shuvoo's avatar

Laravel StrReplace Problem

there have 2 data in database $tr = E , F $ttext = C, D

            foreach ($trigger as $tdata) {
                    $tr = $tdata->trigger;
                    $ttext = $tdata->alttext;
                    $text= str_replace($tr, $ttext, 'A B E F');
//If im echo this result then result is 
Result = A B C F A B E D

                }
            foreach ($trigger as $tdata) {
                    $tr = $tdata->trigger;
                    $ttext = $tdata->alttext;
                    $text= str_replace($tr, $ttext, 'A B E F');
                          }
//If im echo this result then result is 
Result = A B E D

But i want to show A B C D

Thanks In Advance

0 likes
1 reply
Nakov's avatar
Nakov
Best Answer
Level 73

@shuvoo you say you have

$tr = E , F

That should be without comma.. because that's what is different and it cannot be found in the text A B E F in order to be replaced..

>>> str_replace('E F', 'C D', 'A B E F');
=> "A B C D"

This works as it should.

Please or to participate in this conversation.