2013-12-20 48 views
0

好吧我完全堅持這一點,它應該很簡單。複雜的字符串替換

我有兩個陣列:

array(2) { 
[0]=> 
    array(7) { 
    ["id"]=> 
    string(1) "4" 
    ["name"]=> 
    string(9) "Page name" 
    ["content"]=> 
    string(133) "<div class="content col-lg-12">[Jumbo]</div><div class="content col-lg-12">[Jumbo]</div><div class="content col-lg-12">[Footer]</div>" 
    ["container"]=> 
    string(1) "0" 
    ["background"]=> 
    string(7) "#ff7676" 
    ["homepage"]=> 
    string(1) "1" 
    ["active"]=> 
    string(1) "1" 
    } 
    [1]=> 
    array(7) { 
    ["id"]=> 
    string(1) "7" 
    ["name"]=> 
    string(8) "Layout 1" 
    ["content"]=> 
    string(119) "<div class="content col-lg-12">[Header]</div><div class="content col-lg-12"></div><div class="content col-lg-12"></div>" 
    ["container"]=> 
    string(1) "1" 
    ["background"]=> 
    string(7) "#ff7676" 
    ["homepage"]=> 
    string(1) "1" 
    ["active"]=> 
    string(1) "1" 
    } 
} 

和:

array(2) { 
    [0]=> 
    array(9) { 
    ["id"]=> 
    string(1) "8" 
    ["name"]=> 
    string(5) "Jumbo" 
    ["headCSS"]=> 
    string(0) "" 
    ["classes"]=> 
    string(39) "jumbotron module col-lg-6 round-corners" 
    ["inlineCSS"]=> 
    string(144) "background: none repeat scroll 0% 0% rgb(229, 255, 135); border-width: 3px; border-style: solid; border-color: rgb(45, 152, 255); padding: 10px;" 
    ["element"]=> 
    string(247) "    <div style="background: none repeat scroll 0% 0% rgb(229, 255, 135); border-width: 3px; border-style: solid; border-color: rgb(45, 152, 255); padding: 10px;" class="jumbotron module col-lg-6 round-corners">Example</div>   " 
    ["type"]=> 
    string(6) "header" 
    ["size"]=> 
    string(1) "6" 
    ["content"]=> 
    string(7) "Example" 
    } 
    [1]=> 
    array(9) { 
    ["id"]=> 
    string(2) "12" 
    ["name"]=> 
    string(6) "Footer" 
    ["headCSS"]=> 
    string(0) "" 
    ["classes"]=> 
    string(30) "module round-corners col-lg-10" 
    ["inlineCSS"]=> 
    string(64) "background: none repeat scroll 0% 0% transparent; padding: 10px;" 
    ["element"]=> 
    string(143) "<footer style="background: none repeat scroll 0% 0% transparent; padding: 10px;" class="module round-corners col-lg-10"><p>Raaaa!</p> 
</footer>" 
    ["type"]=> 
    string(6) "footer" 
    ["size"]=> 
    string(2) "10" 
    ["content"]=> 
    string(14) "<p>Raaaa!</p> 
" 
    } 
} 

基本上我想替換[XXX]與第一陣列[內容]與第二陣列中[元素]的occurances與數組[名稱]匹配。 (?!):但是

foreach($array1 as $layout) { 
    foreach($array2 as $module) { 
     $needle = "[" . trim($module['name']) . "]"; 
     $pages[$layout['name']] = str_replace($needle, $module['element'], $layout['content']); 
    }; 
}; 

,只有似乎更換第一個數組2元,而不是所有的人都相匹配的內容有意義的希望

到目前爲止,我已經嘗試過這一點。

任何想法最新怎麼了?

作爲一個便箋,iv做了一些閱讀,並認爲這可能會更容易與strtr()但我不知道這是否可能與我的數據。任何建議,這將是偉大的!

+0

當提問用戶'print_r'而不是'var_dump'來顯示信息時。 'var_dump'將不必要的信息添加到難以閱讀的問題中 – chanchal118

+1

@ chanchal118不,'var_dump'更好。 'print_r'不顯示字符串邊界 - 你不知道'$ array2 [1] ['content']' – Barmar

+0

@enigma中有一個換行符爲什麼他需要它?他沒有使用'preg_replace()'。 – Barmar

回答

2

問題是您總是使用$layout['content']作爲str_replace中的主題,而不是更新的$pages[$layout['name']]。所以你沒有合併所有的替代品,你用新的替代品覆蓋舊的替代品。

您可以通過將搜索和替換字符串排列爲str_replace()來一次完成所有替換操作來解決此問題。

$searches = array(); 
$replacements = array(); 
foreach ($array2 as $module) { 
    $searches[] = "[" . trim($module['name']) . "]"; 
    $replacements[] = $module['element']; 
} 
foreach ($array1 as $layout) { 
    $pages[$layout['name']] = str_replace($searches, $replacements, $layout['content']); 
} 
+0

ahhh是的,當然,這是有道理的!好點,謝謝! –