I saw some code from this website:
function link($text){
return preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="$1">$1</a>', $text);
}
What do $0
and $1
mean? Are they regular variables, or do they have special meaning?
The $0
or $1
if used for preg_replace
function;
preg_replace ($pattern , $replacement , $subject)
replacement
may contain references of the form or (since PHP 4.0.4) $n, with the latter form being the preferred one. 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.
from php doc: http://docs.php.net/manual/en/function.preg-replace.php
In the sample URL provided, $1 refers to the first captured sub-pattern in the preg_replace. Refer to the documentation at: http://us1.php.net/preg_replace
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. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern.