<?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)));
- Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.29, 8.2.0 - 8.2.23, 8.3.0 - 8.3.11
- string(173) "?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"
preferences:
66.69 ms | 410 KiB | 5 Q