求指点

WBOY
Release: 2016-06-23 14:13:22
Original
964 people have browsed it

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

$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');
Copy after login


不知道大家的方法会是怎样的,想参照一下.以下我的笨方法
$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;}
Copy after login


回复讨论(解决方案)

$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; $i<count($r[1]); $i++) {    $n = $p = $r[1][$i];    while($t[$p]{0} != '|') $p++;    $t[$p] = "#$n";    $res[$k] = join('', $t);   }}print_r($res);
Copy after login
Array
(
    [m=A1&a=show&id=|之间不管什么1|] => student/#1.html
    [m=Category&type=|之间不管什么1|&page=/] => #1/#22.html
)

你的规则似乎有点问题

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template