Heim  >  Artikel  >  Backend-Entwicklung  >  求该结果的preg_replace的替换写法

求该结果的preg_replace的替换写法

WBOY
WBOYOriginal
2016-06-23 14:05:08848Durchsuche

$url=http://www.xxx.com/66/
preg_replace("#^\/(\d{2,4})\/?$#","name/index.php?t=1&id=$1",strtolower($url));
通过以上我能得到一个重写的网址:name/index.php?t=1&sid=66 

我如何能直接通过一次正则替换,替换出一个数组:如下?
array(
      'name'=>index.php,
      'param'=>array('t'=>1,'id'=66)
     );


回复讨论(解决方案)

一步不行,需要自行分析形成数组。

$url = 'http://www.xxx.com/66/';@preg_replace("#/(\d{2,4})/?$#e","\$t=array('name'=>'index.php','param'=>array('t'=>1,'id'=>$1))",strtolower($url));print_r($t);
66Array
(
    [name] => index.php
    [param] => Array
        (
            [t] => 1
            [id] => 66
        )

)

$url='http://www.xxx.com/66/';$s= preg_replace("#\/(\d{2,4})\/?$#","/name/index.php?t=1&id=$1",strtolower($url));$ar=parse_url($s);$arr['name']=substr($ar['path'],strrpos($ar['path'],'/')+1);parse_str($ar['query'],$t);$arr['param']=$t;print_r($arr);

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn