Concatenate two strings superimposing their intersection substrings

Asked

Viewed 276 times

1

How to 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 blowing the inter substrings between the two getting as follows: http://www.playnow3dgames.com/listing.php?genre=sports&page=1&order=score

following the same reasoning 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 inter 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: inserir a descrição da imagem aqui

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

  • 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.

  • @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

  • This may help: http://php.net/manual/en/function.parse-url.php

  • This does not contemplate the concatenation of some strings

  • 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).

1 answer

1


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
  • this function does not work for the third example

  • Take a look @Ricardohenrique see if this is what you need

  • ,@Marcelobonifazio is not quite this, it should work as follows: when concatenating the first with the second one should pay attention to the fact that the end of the primera is similar to the beginning of the second so we keep only one of the similar parts and concatenate(there may be cases where the end of the first and the beginning of the second are different so nothing should be done), in his reply this changing the value by the bar position, I put some examples (image) in the question see the results (in green) the strings are different from those presented by your answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.