?
Solved

How to work with links in delphi?

Posted on 2011-03-05
6
Medium Priority
?
465 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 38

Accepted Solution

by:
Geert Gruwez earned 2000 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 38

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Simple Linear Regression
Starting up a Project
Suggested Courses

752 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