Solved

How to work with links in delphi?

Posted on 2011-03-05
6
460 Views
Last Modified: 2012-05-11
Hello experts.
please help me.

I need to create a function that eliminate all from a link. To understand me example:
i have a list of website in format (http://www.prokredo.ru/forum/index.php), for example.
This function need to delete all from this link, result need to be this : (http://www.prokredo.ru).
Please help me. I need this.
Function need to delete strings '/', 'forum', 'index', '.php'...but to save the 'http://'

Please, help me.
Thanks
0
Comment
Question by:helyonprime96
6 Comments
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 35043127
you could use posex
delete all as from 3rd /

url = 'http://www.test.com/subtests/index.php'

function findbaseurl(url: string): string;
var n: integer;
begin
  n := posex('/', url, 8);
  if n > 0 then
    delete(url, n, length(url));
  result := url;
end;
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 35043129
you need unit StrUtils;

uses StrUtils;
0
 

Author Comment

by:helyonprime96
ID: 35043140
wow.great.
thanks.

im testing it
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 9

Expert Comment

by:Mahdi78
ID: 35043691
To get the main domain from any link try this

function GetFullMainDomain(Url : string) : string;
var ListDomain : TStringList;
begin
if url = '' then Exit;
ListDomain := TStringList.Create;
  try
  Url := Trim(Url);
  ExtractStrings(['/'], [' '], PChar(Url), ListDomain);
  Result := ListDomain.Strings[0];
  if Pos('https://www.', Url) > 0 then Delete( Url, 1, 12)
  else if Pos('http://www.', Url) > 0 then Delete( Url, 1, 11)
  else if Pos('ftp://www.', Url) > 0 then Delete( Url, 1, 10)
  else if Pos('https://', Url) > 0 then Delete( Url, 1, 8)
  else if Pos('http://', Url) > 0 then Delete( Url, 1, 7)
  else if Pos('ftp://', Url) > 0 then Delete( Url, 1, 6);
  Url := Copy(Url, 1, (Pos('/', Url) - 1));
  ExtractStrings(['.'], [' '], PChar(Url), ListDomain);
  Result := Result +'//www.'+ ListDomain.Strings[ListDomain.Count - 2]+'.'+ ListDomain.Strings[ListDomain.Count - 1];
  finally
  ListDomain.Free;
  end;
end;

For more information take look here
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_25157273.html
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35045016

Don't assume any text positions, search for the text '//' first

function FormatURL(const AUrl: string): string;
var
  Index: Integer;
begin
  Result := Trim(AUrl);
  Index := Pos('//', Result);
  if Index > 0 then
  begin
    Inc(Index, 2);
    Index := PosEx('/', Result, Index);
    if Index > 0 then
      Result := Copy(Result, 1, Index-1);
  end;
end;

test
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(FormatURL('http://www.prokredo.ru/forum/index.php'));
end;
0
 

Author Closing Comment

by:helyonprime96
ID: 35046306
thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question