当前位置: 动力学知识库 > 问答 > 编程问答 >

php - How to change link url image from link other website?

问题描述:

I have a link from other website:

http://www.website1.com/image1.jpg

http://www.website2.com/image/image2.jpg

http://www.website3.com/image/url/image3.jpg

I want change link url from this website, for my webiste:

http://www.mywebsite.com/image1.jpg

http://www.mywebsite.com/image2.jpg

http://www.mywebsite.com/image3.jpg

Can I using regular ex ? How to this ideas?

网友答案:

Try this:

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

Output:

Array
(
    [0] => http://www.website1.com/image1.jpg
    [1] => http://www.website2.com/image2.jpg
    [2] => http://www.website3.com/image3.jpg
)
网友答案:
$new_url = 'http://www.mywebsite.com' + 
substr($url_from_other_website, strrpos($url_from_other_website, '/'));
分享给朋友:
您可能感兴趣的文章:
随机阅读: