What does the $1$2$4 mean in this preg_replace?
Posted
by Taylor
on Stack Overflow
See other posts from Stack Overflow
or by Taylor
Published on 2010-05-22T14:21:54Z
Indexed on
2010/05/22
14:30 UTC
Read the original article
Hit count: 125
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.
© Stack Overflow or respective owner