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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…