1

Guys I have two Strings

$first = "/calvin/master/?p=pages&id=3";   //DYNAMIC URL

$second = "http://localhost/calvin/";      //BASE URL USER DECIDED

I want to get full url like this

$target = "http://localhost/calvin/master/?p=pages&id=3";   //FULL URl

How can I do it.. Please note that, the directory calvin can change according to where user decides, he/she can even place script in child directories. eg. instead of calvin can be myweb/scripts/this_script

NBoymanns
  • 686
  • 6
  • 16
JohnPep
  • 193
  • 1
  • 11
  • `$fullURL = $second . $first;` ? - You'd might need to check whether the two variables contain overlapping parts, but that's a matter of regex or searching the strings. – Epodax Dec 02 '15 at 12:10
  • Can I assume that it's always the first part of `$first` and the last part of `$second`? like `/part/` is a part separated by `/`? – online Thomas Dec 02 '15 at 12:18
  • What is `calvin` in `$first`. Will it always be same as in `$second`? So that `http://localhost/calvin/` becomes `http://localhost/myweb/` and `/calvin/master/?p=pages&id=3` becomes `/myweb/master/?p=pages&id=3`. Or? – Ashish Choudhary Dec 02 '15 at 12:21
  • Yes @AshishChoudhary that is the directory user select to place the script.. – JohnPep Dec 02 '15 at 12:23

3 Answers3

2

This might be what you're trying to do :

<?php


$first = "/calvin/master/?p=pages&id=3";
$second = "http://localhost/calvin/";

//First we explode both string to have array which are easier to compare
$firstArr = explode('/', $first);
$secondArr = explode('/', $second);

//Then we merged those array and remove duplicata
$fullArray = array_merge($secondArr,$firstArr);
$fullArray = array_unique($fullArray);

//And we put all back into a string
$fullStr = implode('/', $fullArray);
?>
Nirnae
  • 1,315
  • 11
  • 23
0
$first_array =split("/" , $first);
$second_array =split("/" , $second);

$url = $second; //use the entire beginning

if($second_array[count($second_array) - 1] == $first_array[0]) { //check if they are the same
    for ($x = 1; $x <= count($first_array); $x++) { //add parts from the last part skipping the very first part
       $url .= "/" .$first_array[$x];
    }
}
online Thomas
  • 8,864
  • 6
  • 44
  • 85
0

You can use:

$target = $second . str_replace(parse_url($second)['path'], '', $first);

parse_url will break the $second URL to obtain the path after the domain which is directory calvin in this case. We can then replace the path in the $first to remove duplicate directory path. and then can append the remaining $first path without the directory to the $second path with the directory.

Ashish Choudhary
  • 2,004
  • 1
  • 18
  • 26