We help IT Professionals succeed at work.

How to parse a URL?

chiwan
chiwan asked
on
Medium Priority
186 Views
Last Modified: 2010-04-06
Comment
Watch Question

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

Author

Commented:
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
Commented:
Try to express your question as a rule.
e.g I want to be able to extract URL number x.
or I want to extract the last URL.

For extracting the Xth URL, use Alan's suggestion.
If you're just after the last URL, just use:

function GetLastURL(const fromURL: string): string;
const
  PROTO = 'http://';
var
  p, i, L: integer;
  s: string;
begin
  L := length(fromURL);
  i := L-7;
  while i > 0 do
  begin
    s := copy(fromURL,i,7);
    if LowerCase(s) = PROTO then
    begin
      result := Copy(fromURL,i,L);
      exit;
    end;
    i := Pred(i);
  end;
  result := fromURL;
end;

Explore More ContentExplore courses, solutions, and other research materials related to this topic.