Well, honestly, I do not know how useful a process of this type would be, it would be easier to pass the new URL to the function that loads the new page, right? but anyway ... I created the following function PHP
:
function alteraURL($urlAntiga, $urlNova){
$url = strstr($urlNova,"?", true);
$url = strstr($urlAntiga,$url, true).$url;
$path = substr(reverse_strrchr($urlAntiga, "/"), 0);
$aux = strstr($urlAntiga,"?", true);
$aux2 = strstr($urlNova,"?", true);
if($aux != "" && $aux != $url){
$url = $path.$aux2;
}
$dadosNovo = strstr($urlNova,"?");
if ($url == ""){
$var1 = substr($urlAntiga, strlen($urlAntiga)- 1);
$var2 = substr($urlAntiga, strlen($urlAntiga)- 1);
if ($var1 == "/" && $var2 == "/") {
$urlAntiga = substr($urlAntiga,0,-1);
}elseif ($var1 == "/" && $var2 == "/"){
$urlAntiga .= "/";
}
$url .= $urlAntiga.$urlNova;
}else{
if ($dadosNovo != ""){
$url .= $dadosNovo;
}
}
return $url;
}
I also used a function that I found on the PHP
official page:
function reverse_strrchr($haystack, $needle){
$pos = strrpos($haystack, $needle);
if($pos === false){
return $haystack;
}
return substr($haystack, 0, $pos + 1);
}
I performed the following tests successfully:
$urlAntiga = "http://www.playnow3dgames.com/listing.php?genre=sports&order=date";
$urlNovo = "listing.php?genre=sports&page=1&order=score";
echo alteraURL($urlAntiga, $urlNovo)."<br/><br/><br/>";
$urlAntiga = "http://www.playnow3dgames.com/testing.php?genre=sports&order=date";
$urlNovo = "listing.php?genre=sports&page=1&order=score";
echo alteraURL($urlAntiga, $urlNovo)."<br/><br/><br/>";
$urlAntiga = "http://www.playnow3dgames.com/listing.php?genre=sports&order=date";
$urlNovo = "testing.php?genre=sports&page=1&order=score";
echo alteraURL($urlAntiga, $urlNovo)."<br/><br/><br/>";
$urlAntiga = "http://www.playnow3dgames.com/";
$urlNovo = "http://www.playnow3dgames.com/game.php?id=cyclomaniacs-epic";
echo alteraURL($urlAntiga, $urlNovo)."<br/><br/><br/>";
$urlAntiga = "http://www.playnow3dgames.com/";
$urlNovo = "/new/16/";
echo alteraURL($urlAntiga, $urlNovo)."<br/><br/><br/>";
$urlAntiga = "MacacoChicleteBanana";
$urlNovo = "BananaLaranjaSorvete";
echo alteraURL($urlAntiga, $urlNovo)."<br/><br/><br/>";
$urlAntiga = "MacacoChicleteBanana";
$urlNovo = "UrsoCamelo";
echo alteraURL($urlAntiga, $urlNovo);
I got the following results:
http://www.playnow3dgames.com/listing.php?genre=sports&page=1&order=score
http://www.playnow3dgames.com/listing.php?genre=sports&page=1&order=score
http://www.playnow3dgames.com/testing.php?genre=sports&page=1&order=score
http://www.playnow3dgames.com/game.php?id=cyclomaniacs-epic
http://www.playnow3dgames.com/new/16/
MacacoChicleteBananaBananaLaranjaSorvete
MacacoChicleteBananaUrsoCamelo