将URL组件变量组合成完整的URL

时间:2011-04-22 23:49:53

标签: php

我想将下面的变量组合成一个名为$ fullurl的新变量。它们是URL的组成部分。组合变量的结构显示在下面的变量下面。我怎么能这样做?

$urlc = nytimes.com/business/finance/articles/2005/05/10/20100727stock-prices-declining.html
$submissiondate = 2005-07-27 2022:38:10
$submittor = mike17
$countcomments = 15
$dispurl = nytimes.com
$submission = News about A Topic
$submissionid = 125
$domainurl = http://www.domain.com/folder/subfolder/index.php?

合并订单:

http://www.domain.com/folder/subfolder/index.php?submission=News+about+A+Topic++&submissionid=125&url=nytimes.com/business/finance/articles/2005/05/10/20100727stock-prices-declining.html&countcomments=15&submittor=mike17&submissiondate=2005-07-27%2022:38:10&dispurl=nytimes.com&subcheck=0

2 个答案:

答案 0 :(得分:1)

最简单的方法是将所有这些放入数组中,然后使用http_build_query。

所以:

$params = array(
   'submittor' => 'mike17',
   'count_comments' => 15,
);

$query_string = http_build_query($params);

$fullurl = $domainurl . $query_string;

为简洁起见,我省略了一些参数。

答案 1 :(得分:0)

如果使用数组而不是分隔变量,则可以通过以下方式构建查询字符串:

$data = array(
          'foo'=>'bar',
          'baz'=>'boom',
          'cow'=>'milk',
       );

$query_string = http_build_query($data);