1、关于路径转换的问题:
   get到某个网站的html文件,网址为:http://www.xxx.com/abc/bcd/def/efg/a.htm
   得到a.htm之后,a.htm中存在一些超链接,例:
   第一种情况:http://www.xxx.com/dd/dd.jpg 这种比较容易。。本身就是绝对路径。
   第二种情况:../../../abc.jpg       如何将这种链接地址转换为绝对地址
   第三种情况:./asdf.jpg             如何将这种链接地址转换为绝对地址
   第四种情况:./asdfsdf.php?id=1000&url=http://www.xxx.com/abc/bcd/d.jpg
                                      如何将这种链接地址转换为绝对地址
   第五种情况:/abc/bcd/kkk.jpg       如何将这种链接地址转换为绝对地址   可能还有漏写了一些,希望大家补上并提供解决办法。2、路轻转换完成之后取得文件名部分
   如上的一,二,三,四种情况转换为绝对地址之后,如何取得文件名称,
   如一中的dd.jpg,二中的abc.jpg,三中的asdf.jpg
   第四种情况肯定要redirect,所以要在redirect之后再行用一,二,三中的方法进行提取   顶贴者有分~~
   解决以上的问题之一的100分。。我会再行开贴给分的。。谢谢   希望大家帮帮忙吧~

解决方案 »

  1.   

    第二种情况:../../../abc.jpg       如何将这种链接地址转换为绝对地址
    --------------------------
    判断../的数量,根据数量从当前路径的末尾向前截取多次/如当前路径是http://www.xxx.com/dd/ee/ff/gg/
    则../../../abc.jpg       的绝对路径是http://www.xxx.com/dd/abc.jpg    第三种情况:./asdf.jpg             如何将这种链接地址转换为绝对地址
    -------------
    取得当前目录,如当前路径是http://www.xxx.com/dd/
    则该绝对路径是http://www.xxx.com/dd/asdf.jpg
       第五种情况:/abc/bcd/kkk.jpg       如何将这种链接地址转换为绝对地址
    --------------------
    在前面加 http://www.xxx.com
      

  2.   

    原理小虫都说了。。编程实现的时候,就是利用pos,copy,ReverseString等字符串处理函数实现。
      

  3.   

    可否写一个函数,该函数的二个参数
    一个就是来源页面,http://www.xxx.com/abc/bcd/def/efg/a.htm
    另一个则是地址,象问题1中的几种情况的地址,如'../../../adsdf.jpg"
    该函数的执行结果应该是:http://www.xxx.com/abc/adsdf.jpg
    当然。这个函数可以处理问题一中的各种情况并输出最终正确的绝对地址
      

  4.   

    还漏了一个
    哪些情况是需要转换的。
    http://开头的肯定不用转了。本身就是绝对地址了
    那么象img.xxx.com/asdf/asdf/a.jpg类的网址。如何判定它是否需要转换?
      

  5.   

    下班了,给你写了两种情况的function GetAbUrl(const CurUrl, FilePath: string; Index: Integer): string;
      function PosLast(const SubStr, Str: string): Integer;
      var
        iCurPos: Integer;
      begin
        Result := 0;
        if SubStr = '' then Exit;
        if Str = '' then Exit;
        iCurPos := PosEx(SubStr, Str);
        while iCurPos > 0 do
        begin
          Result := iCurPos;
          iCurPos := PosEx(SubStr, Str, iCurPos + 1);
        end;
      end;
    var
      iCurPos, iPos: Integer;
      WorkS: string;
    begin
      Result := '';
      if CurUrl = '' then Exit;
      WorkS := CurUrl;
      if WorkS[Length(WorkS)] = '/' then Delete(WorkS, Length(WorkS), 1);
      case Index of
        2:
          begin
            iCurPos := PosEx('../', FilePath);
            iPos := iCurPos;
            while iPos > 0 do
            begin
              WorkS := Copy(WorkS, 1, PosLast('/', WorkS)-1);
              iCurPos := iPos;
              iPos := PosEx('../', FilePath, iCurPos + 1);
            end;
            Result := WorkS + '/' + Copy(FilePath, iCurPos + Length('../'), MaxInt);
          end;
        3:
          begin
            Result := WorkS + '/' + Copy(FilePath, Pos('./', FilePath) + Length('./'), MaxInt);
          end;
        5:
          begin      end;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    beginShowMessage(GetAbUrl('http://www.xxx.com/dd/ee/ff/gg/', '../../../abc.jpg', 2));
    ShowMessage(GetAbUrl('http://www.xxx.com/dd/ee/ff/gg/', './abc.jpg', 3));
    end;
      

  6.   

    给你做了一个,处理了第二,第三,以及第五种情况,剩下的没有弄,要吃饭了。不过大致方法都一样。自己看看吧unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
      private
        { Private declarations }
      public
        function ChangeStr(s,p : string;var filename : string):string;
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
     tm ,fname: string;
    begin
       tm :=ChangeStr('http://www.xxx.com/abc/bcd/def/efg/a.htm','../../../adsdf.jpg',fname);
       ShowMessage(Format('绝对地址:%s;文件名:%s',[tm,fname]));end;function TForm1.ChangeStr(s, p: string;var filename : string): string;
    var
      arrS : array of integer;
      arrp : array of integer;
      i,j,countArrs,countArrp : integer;begin
      countArrs := 0;
      countArrp := 0;
      setlength(arrs,length(s));
      j := 0;
      for i := 0 to length(s)-1 do
        if s[i] = '/' then
        begin
          arrs[j] := i;
          inc(countArrs);
          inc(j);
        end;
      setlength(arrp,length(p));
      j := 0;
      for i := 0 to length(p)-1 do
        if p[i] = '/' then
        begin
          inc(countArrp);
          arrp[j] := i;
          inc(j);
        end;
      filename := copy(p,arrp[countArrp-1]+1,length(p)-arrp[countArrp-1]+1);
      if copy(p,1,2) = '..' then     Result := copy(s,1,arrs[countArrs- countArrp-1]) + filename;
      if copy(p,1,2) = './' then
         Result :=copy(s,1,arrs[countarrs-1]) + filename;  if copy(p,1,1) = '/' then
        Result := copy(s,1,arrs[2])+ filename;end;procedure TForm1.Button2Click(Sender: TObject);
    var
     tm ,fname: string;
    begin
       tm :=ChangeStr('http://www.xxx.com/abc/bcd/def/efg/a.htm','./adsdf.jpg',fname);
       ShowMessage(Format('绝对地址:%s;文件名:%s',[tm,fname]));end;procedure TForm1.Button3Click(Sender: TObject);
    var
     tm ,fname: string;
    begin
       tm :=ChangeStr('http://www.xxx.com/abc/bcd/def/efg/a.htm','/dd.jpg',fname);
       ShowMessage(Format('绝对地址:%s;文件名:%s',[tm,fname]));end;end.
      

  7.   

    谢谢二位的顶力相住。。
    还漏了一个
    哪些情况是需要转换的。
    http://开头的肯定不用转了。本身就是绝对地址了
    那么象img.xxx.com/asdf/asdf/a.jpg类的网址。如何判定它是否需要转换?如果第二个参数是'img.xxx.com/asdf/asdf/a.jpg"。如何判定它不需要转换呢?