MultiplyByZer0 MultiplyByZer0 -4 years ago 56
PHP Question

$n in PHP preg_replace

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?

Answer Source

The $0 or $1 if used for preg_replace function;

preg_replace ($pattern , $replacement , $subject)

replacement may contain references of the form \n 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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download