What is $0 on PHP PCRE functions

2019-05-05 00:52发布

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?

3条回答
我只想做你的唯一
2楼-- · 2019-05-05 01:41

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

查看更多
走好不送
3楼-- · 2019-05-05 01:44

From the PHP manual on preg_replace - http://php.net/manual/en/function.preg-replace.php:

Every such reference will be replaced by the text captured by the n'th parenthesized pattern. n can be from 0 to 99, and \0 or $0 refers to the text matched by the whole pattern.

查看更多
疯言疯语
4楼-- · 2019-05-05 01:51

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.

查看更多
登录 后发表回答