What is $ 0 for PHP PCRE functions?
I have read the documentation about a function preg_filter
that looks like this. This is from php.net site .
$subject = array('1', 'a', '2', 'b', '3', 'A', 'B', '4');
$pattern = array('/\d/', '/[a-z]/', '/[1a]/');
$replace = array('A:$0', 'B:$0', 'C:$0');
print_r(preg_filter($pattern, $replace, $subject));
Here in the $ array, replace some of the available variables - $0
When I try to return this, the value was available before the replacement. Is this a shared variable in PHP or is it only available for PCRE functions? And I saw $1
, $2
, $3
... in some articles.
Usually we cannot have variables starting with numbers.
So can anyone explain this function and variable?
source to share
On the page forpreg_filter
:
preg_filter () is identical to preg_replace (), except that it only returns objects (possibly transformed) that had a match. For more information on how this function works, read the preg_replace () documentation.
On the page forpreg_replace
:
$ 0 refers to text matched by the entire pattern.
source to share
From the PHP manual at preg_replace
- http://php.net/manual/en/function.preg-replace.php :
Each such link will be replaced with text stripped n'th in parentheses. n can range from 0 to 99, and \ 0 or $ 0 refers to text that matches the entire pattern.
source to share