Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the $1$2$4 mean in this preg_replace?

Tags:

Got this function for ammending the query string and was wondering what the replacement part of the pre_replace meant (ie- $1$2$4).

function add_querystring_var($url, $key, $value) {  $url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');  $url = substr($url, 0, -1);  if (strpos($url, '?') === false) {    return ($url . '?' . $key . '=' . $value);  } else {    return ($url . '&' . $key . '=' . $value);  }  } 

Not too familiar with regular expression stuff. I get the various parts to preg_replace but not 100% about the use of '$1$2$4' in the replacement part.

like image 548
Taylor Avatar asked May 22 '10 14:05

Taylor


2 Answers

$1, $2... $n in regular expression replaces are references to the matches wrapped in parenthesis. $0 would be the entire match, $1 would be the first parenthesized capture, $2 would be the second, etc.

  • $1 is a reference to whatever is matched by the first (.*)
  • $2 is a reference to (\?|&)
  • $4 is a reference to the second (.*)

See the docs, specifically the replacement argument of the function:

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. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. To use backslash in replacement, it must be doubled ("\\" PHP string).

like image 58
Andy E Avatar answered Sep 22 '22 05:09

Andy E


Perl regular expression replacement uses match variables which are the parts inside of parentheses in the regular expression:

   $1   $2                      $3 $4 '/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i' 

So, referring to $1 in the replacement string will substitute what was matched in the the first parentheses. $0 however will refer to the entire matching string.

You can even match the parenthetical subsets inside of the regular expression itself using a backslash instead of a dollar sign. For instance, if you wanted to replace doubled words "the", or "and":

preg_replace('/\b(the|and)\b\s*\1/', '$1', $sentence); 
like image 28
amphetamachine Avatar answered Sep 22 '22 05:09

amphetamachine