doz95923 2009-07-21 23:00
浏览 26
已采纳

使用PHP刮掉完整的图像src

I am trying to scrape img src's with php, I can get the src fine, but if the src does not include the full path then I can't really reuse it. Is there a way to grab the full path of the image using php (browsers can get it if you use the right click menu).

ie. How do I get a FULL path including the domain in one of the following two examples?

src="../foo/logo.png"
src="/images/logo.png"

Thanks,

Allan

  • 写回答

2条回答 默认 最新

  • dongyong2906 2009-07-22 01:54
    关注

    You don't need a regex... just some patience. I don't really want to write the code for you, but just check if the src starts with http://, and if not, you have like 3 different cases.

    1. If it begins with a / then prepend http://domain.com
    2. If it begins with .. you'll have to split the full URL and hack off pieces until the src starts with a /
    3. Else (it begins with a letter), the take the full domain, and strip it down to the last slash then append the src URL.

    Or.... be lazy and steal this script

    $url = "http://www.goat.com/money/dave.html";
    $rel = "../images/cheese.jpg";
    
    $com = InternetCombineURL($url,$rel);
    
    //  Returns http://www.goat.com/images/cheese.jpg
    
    function InternetCombineUrl($absolute, $relative) {
        $p = parse_url($relative);
        if($p["scheme"])return $relative;
    
        extract(parse_url($absolute));
    
        $path = dirname($path); 
    
        if($relative{0} == '/') {
            $cparts = array_filter(explode("/", $relative));
        }
        else {
            $aparts = array_filter(explode("/", $path));
            $rparts = array_filter(explode("/", $relative));
            $cparts = array_merge($aparts, $rparts);
            foreach($cparts as $i => $part) {
                if($part == '.') {
                    $cparts[$i] = null;
                }
                if($part == '..') {
                    $cparts[$i - 1] = null;
                    $cparts[$i] = null;
                }
            }
            $cparts = array_filter($cparts);
        }
        $path = implode("/", $cparts);
        $url = "";
        if($scheme) {
            $url = "$scheme://";
        }
        if($user) {
            $url .= "$user";
            if($pass) {
                $url .= ":$pass";
            }
            $url .= "@";
        }
        if($host) {
            $url .= "$host/";
        }
        $url .= $path;
        return $url;
    }
    

    From http://www.web-max.ca/PHP/misc_24.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度