在我的代码中:
<?php
$urls = array('http://www.website1.com/image1.jpg',
'http://www.website2.com/image/image2.jpg',
'http://www.website3.com/image/url/image3.jpg'
);
while(list($i, $uri) = each($urls)) {
$urls[$i] = foo($uri);
}
print_r($urls);
function foo($uri) {
$url = parse_url($uri);
$paths = explode('/', $url['path']);
return sprintf("%s://%s/%s", $url['scheme'], $url['host'], end($paths));
}
?>
输出
Array
(
[0] => http://www.website1.com/image1.jpg
[1] => http://www.website2.com/image2.jpg
[2] => http://www.website3.com/image3.jpg
)
如何修复此结果的输出:
Array
(
[0] => http://www.mywebsite.com/image1.jpg
[1] => http://www.mywebsite.com/image2.jpg
[2] => http://www.mywebsite.com/image3.jpg
)
答案 0 :(得分:2)
将主机字段设置为您想要的任何内容。
function foo($uri)
{
$url = parse_url($uri);
$paths = explode('/', $url['path']);
$url['host'] = 'www.mywebsite.com';
return sprintf("%s://%s/%s", $url['scheme'], $url['host'], end($paths));
}
Array
(
[0] => http://www.mywebsite.com/image1.jpg
[1] => http://www.mywebsite.com/image2.jpg
[2] => http://www.mywebsite.com/image3.jpg
)