D
D
Daniil Sukhikh2020-06-19 20:08:47
PHP
Daniil Sukhikh, 2020-06-19 20:08:47

What does this regular expression mean?

What does this regular expression mean in PHP?
"#^post/(?P\d+)$#"
This part of it is interesting:
(?P\d+)
What does the letter P mean , and in principle this fragment? And what do the brackets mean?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Edward, 2020-06-19
@danchikraw

This is like an incomplete entry of a named subpattern. Incomplete - because there is not enough name in angle (or in apostophes) brackets:

$str = 'post/123';

if (preg_match("#^post/(?P<name>\d+)$#", $str, $arr)) {
    var_dump($arr);
}
The result will be an array:
array (size=3)
0 => string 'post/123' (length=8)
' name ' => string '123' (length=3)
1 => string '123' (length=3 )

W
WinSpik, 2020-06-20
@WinSpik

An expression to search in one string for another, while not a direct match, such as in the strpos function, but with a bunch of options.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question