热搜:NVER node 开发 php

求指点

2024-09-15 16:40:01
求指点

我用了个笨方法将以下数组

$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
)

你的规则似乎有点问题

没有的,呵呵,你不太了解详细而已,没事了,这是我们项目具体的事了.谢谢哦!