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

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题