Concatenate two strings by overlapping their intersecting substrings

1

How do I insert the string listing.php?genre=sports&page=1&order=score  at the end of the string http://www.playnow3dgames.com/listing.php?genre=sports&order=date subtracting the substrings of the interaction between the two being as follows: http://www.playnow3dgames.com/listing.php?genre=sports&page=1&order=score

following the same rationale concatenate this string http://www.playnow3dgames.com/ with this other game.php?id=cyclomaniacs-epic resulting in: http://www.playnow3dgames.com/game.php?id=cyclomaniacs-epic

The area of the interaction substring should be only between the end of the first and the beginning of the second. and how to check if there is an intersection area (at the end and at the beginning) between two strings

Image describing the process:

In red the area of intersection between the two strings to be concatenated, in green the result.

    
asked by anonymous 12.02.2015 / 12:37

1 answer

1

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
    
12.02.2015 / 16:01