2015-02-06 88 views
-2

我有刮图片的代码,网址是相对前:如何将相对网址修改为绝对网址?

/upload/produse/Blu-ray/BD-H6500.jpg?maxwidth=800&maxheight=800 

和我的魔杖变成绝对URL例如:

example.com/upload/produse/Blu-ray/BD-H6500.jpg 

这里是我的代码:

if(isset($this->request->post['image_scrapper'])){ 
       $imageQuery = $xpath->query("//div[@id='fancybox-content']//img/@src");          
       if($imageQuery->length > 0){ 
        foreach ($imageQuery as $key => $image){ 
         $extensie = end((explode('/', $image->nodeValue))); 
         $extensie = end((explode('.', $extensie))); 
         $model = preg_replace("/[^a-zA-Z0-9 -\/]+/","",trim($this->request->post['model'])); 
         $imageName = str_replace(" ","-",trim($this->request->post['product_description']['1']['name'])); 
         $imageName = preg_replace("/[^a-zA-Z0-9 -]+/","",$imageName); 
         $imageName .= "-".preg_replace("/[^a-zA-Z0-9 -]+/","",$model); 
         $imageName = strtolower($imageName); 
         $imageName = preg_replace("/[-]+/","-",$imageName); 
         $imageName .= '-'.rand().'.'.$extensie; 
         $imageName = 'data/'.$imageName; 

         copy($image->nodeValue, DIR_IMAGE.$imageName); 
         if($key == '0'){ 
          $this->model_catalog_product->firstImage($product_id, $imageName); 
         }else{ 
          $this->model_catalog_product->aditionaleImage($product_id, $imageName); 
         } 
        } 
       } 
      } 

在此先感谢您的帮助。

+0

看看[phpUri(https://github.com/monkeysuffrage/phpuri) – pguardiario 2015-02-06 06:43:43

回答

0

试试这个:

<?php 
function url_relative_to_absolute($baseUrl, $relativeUrl, $removeGet=true) { 
    // concat baseurl 
    // (in most cases it's server's host aka $_SERVER[HTTP_HOST]) and relative url 
    $url = rtrim($baseUrl, '/').'/'.ltrim($relativeUrl, '/'); 
    // remove get params 
    if($removeGet) 
     $url = strtok($url, '?'); 
    return $url; 
} 

例子是在这里:ideone

+0

谢谢回答@userlond,好回答 – 2015-02-06 23:07:08