<?php
// Everything you enter here will be executed by our servers. Try it!
$uri = "?sort%5B%5D=1&str%5B%5D=4&curs%5B%5D=1&page=2&page=1&page=2&page=5&page=4&page=3&page=2&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&tesdsd=2";
function sgp($url, $varname, $value) // substitute get parameter
{
if (is_array($varname)) {
foreach ($varname as $i => $n) {
$v = (is_array($value))
? ( isset($value[$i]) ? $value[$i] : NULL )
: $value;
$url = sgp($url, $n, $v);
}
return $url;
}
preg_match('/^([^?]+)(\?.*?)?(#.*)?$/', $url, $matches);
$gp = (isset($matches[2])) ? $matches[2] : ''; // GET-parameters
if (!$gp) return $url;
$pattern = "/([?&])$varname=.*?(?=&|#|\z)/";
if (preg_match($pattern, $gp)) {
$substitution = ($value !== '') ? "\${1}$varname=" . preg_quote($value) : '';
$newgp = preg_replace($pattern, $substitution, $gp); // new GET-parameters
$newgp = preg_replace('/^&/', '?', $newgp);
}
else {
$s = ($gp) ? '&' : '?';
$newgp = $gp.$s.$varname.'='.$value;
}
$anchor = (isset($matches[3])) ? $matches[3] : '';
$newurl = $matches[1].$newgp.$anchor;
return $newurl;
}
var_dump(sgp($uri,array('page'),array(1)));
preferences:
19.35 ms | 407 KiB | 5 Q