2015-12-02 64 views
1

傢伙,我有兩個字符串PHP兩個字符串來獲得完整的URL

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

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

我想獲得完整的URL這樣

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

我怎樣才能做到這一點。請注意的是,目錄calvin可以根據用戶決定的位置進行更改,他/她甚至可以將腳本放置在子目錄中。例如。而不是calvin可以myweb/scripts/this_script

+0

'$ fullURL = $ second。 $ first;'? - 您可能需要檢查兩個變量是否包含重疊部分,但這是一個正則表達式或搜索字符串的問題。 – Epodax

+0

我可以假設它始終是'$ first'的第一部分,'$ second'的最後一部分?像'/ part /'是由'/'分隔的部分? –

+0

'$ first'中的'calvin'是什麼?它會和'$ second'一樣嗎?所以'http:// localhost/calvin /'變成'http:// localhost/myweb /'和'/ calvin/master /?p = pages&id = 3'成爲'/ myweb/master /?p = pages&id = 3 '。要麼? –

回答

2

這可能是你想做什麼:

<?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); 
?> 
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]; 
    } 
} 
0

您可以使用:

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

parse_url將打破$second網址在這種情況下獲得目錄calvin的域之後的路徑。然後,我們可以替換$first中的路徑以刪除重複的目錄路徑。然後可以將目錄中沒有目錄的其餘$first路徑附加到$second路徑。