Zhaf
Zhaf

Reputation: 1884

Regular expression (preg_match) in PHP

Just simple question (I've google this but not found the answer). What's the meaning of ?P in this statement :

preg_match('/^posts\/(?P<id>\d+)$/', $url, $matches);

Upvotes: 1

Views: 112

Answers (2)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

It captures the match of id into the backreference "<name>" which is <id> in this case. Check this link for more clearification: http://php.net/manual/en/function.preg-match.php

Upvotes: 0

Paul
Paul

Reputation: 141839

(?P<name>patt) is a named subpattern. It means that you can access the match for that subpattern by looking in $matches['id'] as well as the usual $matches[1].

Upvotes: 1

Related Questions