在setting sun的博客上发现一篇 文章,他认为:
$p = "/^(?<ip>.*) (?<time>.*)$/";
$s = "10.4.0.111 2009-09-09";
preg_match($p, $s, $m);
print_r($m);
输出:
Array ( [0] => 10.4.0.111 2009-09-09 [ip] => 10.4.0.111 [1] => 10.4.0.111 [time] => 2009-09-09 [2] => 2009-09-09 )
这个数组中有ip,time的key,他认为这是PHP5.3的功能。回复中echo说没什么特别。
事实上,这个还真不是php5.3的功能,它是PHP4.3开始就有的功能。详情可以看手册:
Pattern Syntax -- Describes PCRE regex syntax这一章。其中有一段就是:
本想回复到setting sun的博客上的,但。。。后来打不开了,原文网址为:http://www.setting.cc/blog/archives/2.html
It is possible to name the subpattern with (?P<name>pattern) since PHP 4.3.3. Array with matches will contain the match indexed by the string alongside the match indexed by a number, then.
所以,这还真不是啥新功能,只是我们很少用到而已。顺便说一句,今天的lamp分享中,祁宏还真的提到了这一点(关于分享内容我写了博客,但祁宏讲的内容,我没有详细记录参加lamp聚会有感 ),所以我才特别有印象。本想回复到setting sun的博客上的,但。。。后来打不开了,原文网址为:http://www.setting.cc/blog/archives/2.html

评论 (0)
暂无评论