Link to home
Start Free TrialLog in
Avatar of chiwan
chiwan

asked on

How to parse a URL?

Avatar of alanwhincup
alanwhincup

Here is one way you could do it:

function GetURL(S : string; URLPos : Integer) : string;
var
  I, BPos, EPos : Integer;
  URL : string;
begin
  I := 0;
  URL := S;
  while I <> URLPos do
  begin
    BPos := Pos('http://', URL);
    URL := Copy(URL, BPos + 7, Length(URL) - BPos);
    Inc(I);
  end;
  if Pos('http://', URL) <> 0 then
  begin
    EPos := Pos('http://', URL);
    Result := 'http://' + Copy(URL, 0, EPos - 1);
  end
  else
    Result := 'http://' + URL;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(GetURL('http://www.firstdomain.com/cgi-bin/serv?url=http://www.seconddomain.com/product.asp?url=http://www.thirddomain.com/detail.asp?pid=40987', 3));
end;

With the above example you can extract any URL from a string like you gave in  your question.

Cheers,

Alan
Avatar of chiwan

ASKER

Use 3 as parameter is not good idea, because sometime we don't know how many http:// in orginal URL string, what I want to do is just retrive out the last http://, how to do that?

Thanks

Chiwan
ASKER CERTIFIED SOLUTION
Avatar of rondi
rondi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial