我用了個笨方法將以下數組
$rules = array('student/|之間不管什麼1|.html' => 'm=A1&a=show&id=#1','|之間不管什麼1|/|之間不管什麼|2.html' => 'm=Category&type=#1&page=#2');//處理成這樣一個結果數組:$rules = array('m=A1&a=show&id=|之間不管什麼1|' => 'student/#1.html','m=Category&type=|之間不管什麼1|&page=|之間不管什麼2|' => '#1/#2.html');
不知道大家的方法會是怎樣的,想參照一下.以下我的笨方法
$aRule = array(); //結果//反轉規則與URLforeach($rules as $key => $value){$reg = '#\|.*\|#U';$matchCount = 0; //匹配到的數量$aMatchResult = array(); //匹配到的內容$matchCount = preg_match_all($reg, $key, $aMatchResult);if(!$matchCount){continue;}$aMatchResult = $aMatchResult[0]; //取內容//根據內容數量產生相應條數的#n$aPartern = array();for ($i = 0; $i < count($aMatchResult); $i++){$aPartern[] = '#' . ($i + 1);}$tmpKey = str_replace($aPartern, $aMatchResult, $value); //將value裡的一個個#1, #2.. #n換成對應的括弧$tmpValue = str_replace($aMatchResult, $aPartern, $key); //將key裡的一個個 |豎號1|, |豎號2|.. |豎號n|換成對應的 #n$aRule[$tmpKey] = $tmpValue;}
回複討論(解決方案)
$rules = array( 'student/|之間不管什麼1|.html' => 'm=A1&a=show&id=#1', '|之間不管什麼1|/|之間不管什麼|2.html' => 'm=Category&type=#1&page=#2');foreach($rules as $k=>$v) { $t = preg_split('/(\|[^|]+\|)/', $k, -1, PREG_SPLIT_DELIM_CAPTURE); preg_match_all('/#(\d+)/', $v, $r); $k = preg_replace('/#(\d+)/e', '$t[$1]', $v); for($i=0; $iArray
(
[m=A1&a=show&id=|之間不管什麼1|] => student/#1.html
[m=Category&type=|之間不管什麼1|&page=/] => #1/#22.html
)
你的規則似乎有點問題 沒有的,呵呵,你不太瞭解詳細而已,沒事了,這是我們項目具體的事了.謝謝哦!