从其他链接修复链接url mywebsite

时间:2011-12-15 04:15:35

标签: php

在我的代码中:

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

1 个答案:

答案 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));
}

Output

Array
(
    [0] => http://www.mywebsite.com/image1.jpg
    [1] => http://www.mywebsite.com/image2.jpg
    [2] => http://www.mywebsite.com/image3.jpg
)