Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is $0 on PHP PCRE functions

I read a documentation of preg_filter function it is look as follows. 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 of $replace some variables available like this - $0 When I try this it is returning the value was available before replace. Is it a common variable on PHP or is it only available for PCRE functions? And i seen $1, $2, $3 ... also in some articles.

Normally we can't have variables starting with numbers.

So can any one explain about this function and variable?

like image 869
sugunan Avatar asked Feb 22 '23 01:02

sugunan


2 Answers

$0 represents the entire part of the string that matches the pattern. $1 and so on represent the subpatterns.

like image 118
Niet the Dark Absol Avatar answered Mar 02 '23 00:03

Niet the Dark Absol


From the manual page for preg_filter:

preg_filter() is identical to preg_replace() except it only returns the (possibly transformed) subjects where there was a match. For details about how this function works, read the preg_replace() documentation.

From the manual page for preg_replace:

$0 refers to the text matched by the whole pattern.

like image 38
Oliver Charlesworth Avatar answered Mar 02 '23 01:03

Oliver Charlesworth