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 = "http://fashion/ru/items/2?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, $url)) { $substitution = ($value !== '') ? "\${1}$varname=" . preg_quote($value) : ''; $newgp = preg_replace($pattern, $substitution, $url); // new GET-parameters $newgp = preg_replace('/^&/', '?', $newgp); } else { $s = ($url) ? '&' : '?'; $newgp = $url.$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: matches in /in/FEuaS on line 29 string(198) "http://fashion/ru/items/2?sort%5B%5D=1&str%5B%5D=4&curs%5B%5D=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&tesdsd=2"
Output for 4.3.0 - 5.0.3
Notice: Undefined variable: matches in /in/FEuaS on line 29 string(198) "http://fashion/ru/items/2?sort%5B%5D=1&str%5B%5D=4&curs%5B%5D=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&page=1&tesdsd=2"