Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
542 views
in Technique[技术] by (71.8m points)

Replace string by values from collection in Laravel

I am beginner ini Laravel. I have this code:

    $value = "Szanowni Państwo,
    Status zosta? zmieniony.
    <br/><br/>
    
    Osoba odp.: {osoba_odpowiedzialna}<br/>";
    
$collection = collect(
            (object) [
                'osoba_odpowiedzialna' => $responsiblePerson,
                'rodzaj' => data_get($term, 'termType.name'),
                'klient' => data_get($term, 'client.name'),
                'sprawa' => data_get($term, 'caseInstance.internal_signature'),
                'status' => data_get($term, 'termStatus.name'),
                'adres' => route('calendar.index')
            ]
        );

in result $collection I have:

https://ibb.co/Kz18CJ1

I need replace my $value - values from $collection by key: osoba_odpowiedzialna, klient, rodzaj etc.

How can I make it?

question from:https://stackoverflow.com/questions/65626214/replace-string-by-values-from-collection-in-laravel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your question is not very clear, sorry, but I think what you want is this:

$replacedText = preg_replace('/{osoba_odpowiedzialna}/', 
$collection['osoba_odpowiedzialna'], $value);

//this will yield (last line below)
//Osoba odp.: ?ukasz Moderator

After your comments:

$collection->map(function($item, $key) use (&$value){  //$collection->each(.. should also be fine
  $value = preg_replace('/{'.$key.'}/', $item, $value);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...