3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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; } $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 5.0.4 - 7.1.0
Notice: Undefined variable: gp in /in/aVTbr on line 18 Notice: Undefined variable: gp in /in/aVTbr on line 24 Notice: Undefined variable: gp in /in/aVTbr on line 25 Notice: Undefined variable: matches in /in/aVTbr on line 29 string(7) "?page=1"
Output for 4.3.0 - 5.0.3
Notice: Undefined variable: gp in /in/aVTbr on line 18 Notice: Undefined variable: gp in /in/aVTbr on line 24 Notice: Undefined variable: gp in /in/aVTbr on line 25 Notice: Undefined variable: matches in /in/aVTbr on line 29 string(7) "?page=1"