回答PHPCHINA上的几个问题:URL映射

  <?

  /**

  *对url路由进行简单的解析,支持对/path/to/site/index.php/module/action/parm/value

  * /path/to/site/index.php?/module/action/parm/value和

  * /path/to/site/?/module/action/parm/value三种形式的处理

  *@param:null

  *@return:router array

  */

  function url_router(){

  $path=strip_tags($_SERVER['REQUEST_URI']);

  $strpos=strpos($path,'.php');

  if($strpos){

  $path=substr($path,$strpos+4);

  }else{

  if(empty($_SERVER['QUERY_STRING'])){

  $strpos=strpos($path,'?');

  if($strpos){

  $path=substr($path,$strpos+1);

  }else{

  $path='';

  }

  }else{

  $path=$_SERVER['QUERY_STRING'];

  }

  }

  //统一化$path的格式,如果$path的第一个字符为/则去掉

  if($path[0]=='/'){

  $path=substr($path,1);

  }

  //解析,并且路由

  if(!empty($path)){

  $path=explode('/',$path);

  $router['controller']=$path[0];

  $router['action']=(!empty($path[1]))?$path[1]:'index';

  //print_r($path);

  for($i=2;$i

  $params[$path[$i]]=(isset($path[$i+1]))?$path[$i+1]:'';

  }

  $router['params']=$params;

  }else{

  //默认路由信息

  $router['controller']='index';

  $router['action']='index';

  $router['params']=array();

  }

  return $router;

  }

  ?>