您的位置:首页 > 编程语言 > PHP开发

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

2017-01-05 15:07 274 查看
示例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/(?<type>\w+)/id/(?<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', ) )
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: