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

4
  • $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. Commented Dec 2, 2015 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 /? Commented Dec 2, 2015 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? Commented Dec 2, 2015 at 12:21
  • Yes @AshishChoudhary that is the directory user select to place the script.. Commented Dec 2, 2015 at 12:23

3 Answers 3

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);
?>
Sign up to request clarification or add additional context in comments.

Comments

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];
    }
}

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.