Solved

How to work with links in delphi?

Posted on 2011-03-05
6
462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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 entity bean example 2 54
How to make Sign in, using Clientdataset? 1 40
Firemonkey BASS_Init into a thread 17 78
Java array 10 85
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.

739 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