php中正则表达式的分组和命名

示例1:

 $regex='#^/type/(\w+)/id/([0-9]+)$#i';

 $str='/type/topic/id/11';

 if(preg_match($regex,$str,$matches)){
     var_export($matches);
 }
result:
array ( 
    0 => '/type/topic/id/11', 
    1 => 'topic', 
    2 => '11', 
    )

示例2:

 $regex='#^/type/(?\w+)/id/(?[0-9]+)$#i';

 $str='/type/topic/id/11';

 if(preg_match($regex,$str,$matches)){
     var_export($matches);
 }
result:
array ( 
    0 => '/type/topic/id/11', 
    'type' => 'topic', 
    1 => 'topic', 
    'id' => '11', 
    2 => '11', ) )

你可能感兴趣的:(学习笔记)