在laravel中使用preg_replace

I have database data like text [mydata] some text [otherdata] some more text and I want to replace those [mydata] , [otherdata] with dynamic info such as category title for instance.

So the result of my data would be like:

text Category Title some text Category Desc some more text

instead of

text [mydata] some text [otherdata] some more text

I think that can be happen by preg_replace but not quite sure.

code

View::composer('*', function ($view) {
  $notes = Notes::all();
  foreach($notes as $note){
     $descrip= $note->description;
  }

  $view->with('descrip', $descrip);
});

More

So basically $note->description content is this data:

text [mydata] some text [otherdata] some more text

I want to replace those elements by data from categories table.

Any idea?

Update

well i was digging and get code below (useing str_replace) however it has some issues,

View::composer('*', function ($view) {
            //getting categories and pull out the values i want to use as `[....]`
            $catC = Category::all();
            foreach($catC as $catD){
                $meta_title = $catD->title;
                $meta_desc = $catD->meta_description;
                $meta_tags = $catD->meta_tags;
            }
            //replace those categories values with element in database `[......]`
            $seotemplates = SeoTemplate::all();
            foreach($seotemplates as $seotemplate){
                $myCustom = str_replace_array('[cattitle]', [$meta_title, $meta_desc, $meta_tags], $seotemplate->categories_desc_template);
            }

            $view->with('myCustom', $myCustom);
        });

Issues

  1. I can only get [cattitle] but what about [catmeta] & [cattags]?
  2. $meta_title will always return first value from categories table (for example if i'm visiting HP category page it return ACER as [cattitle] or any other category pages i visit. It's always ACER

Update 2

I solved the issue 1 of my first update but still issue 2 remained, here is updated code

View::composer('*', function ($view) {
            //categories
            $catC = Category::all();
            $catD = [];
            foreach($catC as $catD){
                $catD = [
                    $cattitle = $catD->title,
                    $catmeta = $catD->meta_tags,
                    $catdesc = $catD->meta_description
                ];
            }
            $a1 = array("[cattitle]","[catmeta]","[catdesc]");

            $seotemplates = SeoTemplate::all();
            foreach($seotemplates as $seotemplate){
               $myCustom = str_replace($a1, $catD, $seotemplate->categories_desc_template);
             }

            $view->with('myCustom', $myCustom);
});

Current issue:

  1. $meta_title will always return first value from categories table (for example if i'm visiting HP category page it return ACER as [cattitle] or any other category pages i visit. It's always ACER

I see a couple of issues with your code that I think will help with getting the solution that you are after.

First like mentioned by @hktang said in his comment you are duplicating your variables and then assigning it's value over and over again instead of adding to it. Try this:

$catE = [];
foreach($catC as $catD){
    $catE[] = [
        $cattitle = $catD->title,
        $catmeta = $catD->meta_tags,
        $catdesc = $catD->meta_description
    ];
}

Second you are also resetting the value of $myCustom with each loop of the for each. Try this:

$myCustom = []  
foreach($seotemplates as $seotemplate){
    $myCustom[] = str_replace($a1, $catE, $seotemplate->categories_desc_template);
}

This should result in you getting an array of seotemplates with the values replaced but I have not testing this.