如何在报废时修改相对url到绝对url?

时间:2015-02-06 04:14:12

标签: php url screen-scraping relative

我有这个代码用于抓取图像,而url是相对的ex:

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

我将魔杖改为绝对网址:

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

提前感谢您的帮助。

1 个答案:

答案 0 :(得分: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