Well, honestly, I don’t know what use a process of this kind 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 I found on the official website of PHP
:
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
What determines change? is always what will come after the first
.php
that is found? You need to find a pattern to choose the point of change.– Maniero
@bigown should be as follows: the initial string should always be complete (http://....) and we insert the second string at the end of the first string, note that the beginning of the second coincides with the end of the first so we must remove this similar part of the first or the second and concatenate the end of the second, note that when not the similarities as in the second example there is nothing to remove
– Ricardo
This may help: http://php.net/manual/en/function.parse-url.php
– Lucas
This does not contemplate the concatenation of some strings
– Ricardo
I think if you put the real problem to rest, it would make it easier to come up with good solutions. Even because the string overlay is not a solution for parameter merging, there seems to be a confusion of concepts in the question, which perhaps for the author has some meaning, but for those who read it can generate doubts (or suddenly not be the ideal solution).
– Bacco