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

Shuvoo's avatar

Laravel Str::replaceArray

In database

print_r( $result );
Array ( [0] => Array ( [trigger] => {mytext} [alttext] => My text 1 ) [1] => Array ( [trigger] => {mytext2} [alttext] =>My text 2 ) )
$text = '{mytext1} {mytext2}';
 $trigger = Trigger::select('trigger', 'alttext')->where('campaign_id', $campaignid)->get();
                $result = $trigger->toArray();
                foreach ($result as $tdata) {
                    $tr = $tdata['trigger'];
                    $ttext = $tdata['alttext'];
                    $replace = Str::replaceArray($tr, [$ttext], $messagebody);
                    echo $replace;
                }

Result is

{mytext1} My text 2 My text 1 {mytext2} 

i want to show result

My text 1 My text 2
0 likes
2 replies
Snapey's avatar

I think you have no answers because its not possible to understand your code

bugsysha's avatar

@shuvoo first trigger is {mytext} instead of {mytext1}. Can it be that this is a dummy example and not real code that you've provided us with? Maybe you have more records in the database which have wrong values/typos?

Please or to participate in this conversation.