Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从两个字符串获取完整url_Php - Fatal编程技术网

PHP从两个字符串获取完整url

PHP从两个字符串获取完整url,php,Php,伙计们,我有两条线 $first = "/calvin/master/?p=pages&id=3"; //DYNAMIC URL $second = "http://localhost/calvin/"; //BASE URL USER DECIDED 我想得到完整的网址像这样 $target = "http://localhost/calvin/master/?p=pages&id=3"; //FULL URl 我该怎么做。。请注意,目录calvin可以

伙计们,我有两条线

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

$second = "http://localhost/calvin/";      //BASE URL USER DECIDED
我想得到完整的网址像这样

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

我该怎么做。。请注意,目录
calvin
可以根据用户决定的位置进行更改,他/她甚至可以将脚本放在子目录中。例如,代替
calvin
可以是
myweb/scripts/this\u script
这可能是您想要做的:

<?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);
?>

$first\u数组=拆分(“/”,$first);
$second_数组=拆分(“/”,$second);
$url=$second//使用整个开头
如果($second_array[count($second_array)-1]=$first_array[0]){//检查它们是否相同
对于($x=1;$x您可以使用:

$target=$second.str\u replace(解析url($second)['path'],''$first);


parse_url
将中断
$second
url,以获取域后的路径,在本例中为目录
calvin
。然后我们可以替换
$first
中的路径以删除重复的目录路径。然后可以将剩余的
$first
路径(不带目录)附加到
$second目录路径。

$fullURL=$second.$first;
?-您可能需要检查这两个变量是否包含重叠部分,但这是正则表达式或搜索字符串的问题。我是否可以假设它始终是
$first
的第一部分和
$second
的最后一部分?如
/part/
是由
/
分隔的部分吗?
$first
中的
calvin
是什么。它是否总是与
$second
中的相同?以便
http://localhost/calvin/
变为
http://localhost/myweb/
/calvin/master/?p=pages&id=3
变成
/myweb/master/?p=pages&id=3
。或者?是@AshishChoudh这是用户选择用来放置脚本的目录。。
$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];
    }
}