使用PHP刮除完整图片src [英] Scrape FULL image src with PHP

查看:105
本文介绍了使用PHP刮除完整图片src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用php刮除img src的问题,我可以使src很好,但是如果src不包括完整路径,那么我将无法真正重用它.有没有一种方法可以使用php捕获图像的完整路径(如果使用右键单击菜单,浏览器就可以获取图像).

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"

谢谢

艾伦

推荐答案

您不需要正则表达式...只是有些耐心.我并不是真的想为您编写代码,但是只需检查src是否以http://开头,否则,您会遇到3种不同的情况.

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. 如果以/开头,则在 http://domain.com
  2. 之前添加
  3. 如果它以..开头,则必须拆分完整的URL和破解直到src以/
  4. 开头
  5. 其他(以字母开头),完整域名和将其向下拖至最后一个斜杠,然后附加src URL.
  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;
}

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

这篇关于使用PHP刮除完整图片src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆