簡體   English   中英

在Laravel中使用preg_replace

[英]Using preg_replace in laravel

我有數據庫數據,例如text [mydata] some text [otherdata] some more texttext [mydata] some text [otherdata] some more text並且我想用動態信息(例如類別標題)替換那些[mydata][otherdata]

因此,我的數據結果如下所示:

text Category Title some text Category Desc some more text

代替

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

我認為可以通過preg_replace來實現,但不是很確定。

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

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

更多

因此,基本上$note->description內容是以下數據:

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

我想用categories表中的數據替換那些元素。

任何想法?

更新

我正在挖掘並在下面獲取代碼(使用str_replace ),但是它存在一些問題,

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);
        });

問題

  1. 我只能得到[cattitle]但是[catmeta] & [cattags]呢?
  2. $meta_title將始終從類別表返回第一個值(例如,如果我正在訪問HP類別頁面,則它以[cattitle]或我訪問的任何其他類別頁面返回ACER 。它始終是ACER

更新2

我解決了第一次更新的問題1,但仍然存在問題2,這里是更新的代碼

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);
});

目前的問題:

  1. $meta_title將始終從類別表返回第一個值(例如,如果我正在訪問HP類別頁面,則它以[cattitle]或我訪問的任何其他類別頁面返回ACER 。它始終是ACER

我發現您的代碼有幾個問題,這些問題將有助於您尋求解決方案。

首先,就像@hktang提到的那樣,在他的評論中,您正在復制變量,然后一遍又一遍地分配它的值,而不是添加它。 嘗試這個:

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

其次,您還需要通過每個循環來重置$ myCustom的值。 嘗試這個:

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

這應該導致您獲得替換值的seotemplates數組,但我尚未對此進行測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM