Solved

How to work with links in delphi?

Posted on 2011-03-05
6
461 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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:Ephraim Wangoya
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 69
Breakpoint doesn't stop in my variable 3 36
How to Have Macro Files Automatically "Edit" 5 45
Firemonkey Queue list 1 25
This article will show, step by step, how to integrate R code into a R Sweave document
A short article about problems I had with the new location API and permissions in Marshmallow
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

685 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