2015-12-02 35 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路径。

相关问题