简体   繁体   English

PHP URL解析和编辑

[英]Php url parsing and editing

I searched "the whole" stackoverflow but didn't find a decent answer that works for me. 我搜索了“整个” stackoverflow,但没有找到合适的答案。 I need to change the host of a url in php. 我需要在php中更改网址的主机。

This url: http://example123.com/query?t=de&p=9372&pl=bb02799a&cat=&sz=400x320&scdid=e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24&rnd=29137126 此网址: http : //example123.com/query? t= de& p= 9372& pl= bb02799a&cat=& sz= 400x320& scdid= e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24& rnd= 29137126

To This: http://example456.com/test?t=de&p=9372&pl=bb02799a&cat=&sz=400x320&scdid=e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24&rnd=29137126 为此: http : //example456.com/test?t=de&p=9372&pl=bb02799a&cat=&sz=400x320&scdid=e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24&rnd=29137126

I only need to change the domain and the path or file, so far I've got this: 我只需要更改域和路径或文件,到目前为止,我已经做到了:

$originalurl = http://example123.com/query?t=de&p=9372&pl=bb02799a&cat=&sz=400....
$parts = parse_url($originalurl);
$parts['host'] = $_SERVER['HTTP_HOST'];
$parts['path'] = '/test';
$modifiedurl = http_build_query($parts);
print_r(urldecode($modifiedurl));

but it echos 但它呼应

scheme=http&host=localhost&path=/test&query=t=de&p=9372&pl=bb02799a&cat=&sz=400... scheme = http&host = localhost&path = / test&query = t = de&p = 9372&pl = bb02799a&cat =&sz = 400 ...

Please I don't want to use some strpos or something like that as I need it to be variable. 请不要使用一些strpos之类的东西,因为我需要它是可变的。 Thanks ;) 谢谢 ;)

You'll have to do some concatenating manually. 您必须手动进行一些串联。 This works: 这有效:

$originalurl = "http://example123.com/query?t=de&p=9372";
$parts = parse_url($originalurl);
$new_path = '/test';
$modifiedurl = $parts['scheme'] . "://" . $_SERVER['HTTP_HOST'] . $new_path . (isset($parts['query']) ? "?".$parts['query']:"");
print_r($modifiedurl);
$url = 'http://example123.com/query?t=de&p=9372&pl=bb02799a&cat=&sz=400x320&scdid=e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24&rnd=29137126';
$query = parse_url($url)['query'];

$newUrl = 'http://www.younewdomain.com/path?' . $query;

Came up with a different approach: 提出了另一种方法:

$url = "http://example123.com/query?t=de&p=9372&pl=bb02799a&cat=&sz=400x320&scdid=e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24&rnd=29137126";
$new_host = "http://newhost.com/blab";

//explode at ? so you get the query
$split = explode("?",$url,2);

//build new url
$new_url = $new_host."?".$split[1];

//finish
echo $new_url;

parse_url()的反向函数应该是http_build_url() ,您是否尝试过?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM