get website/webpage css link from html code

Hi all,

I need a way to get the css file link from some html code...say in a memo...

Here is what it looks like:

<link rel="stylesheet" type="text/css" href="style.css">

These are the rules/what it needs to look for:

First we need to know it's a css link or external file link...so it looks for:

this: <link

next we get the filename I need so we look here:

We look after ths --> href="

and before this -->  ">

And this is what I need to be left with: style.css

Hope you can help

thx

st3vo
ST3VOAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nordtorpCommented:
I am no delphi programmer, but this might help a little bit. I know this is for a <a> tag, but it might give you some thoughts.

//find the link
iStart:=Pos('<a href="',ItemBuf) + Length('<a href="');
iStop:=Pos('">',ItemBuf);

Code got from this link:

Simple HTML page Scraping with Delphi
http://delphi.about.com/library/code/ncaa062502a.htm
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
This is exactly what you're asking for, btw I'd use "stylesheet" as key beacause <link could contain the link for the icon...

Let me know if you need to change it for looking for the word "stylesheet"
function ExtractCSSFileName(Text: String): string;
var
  AUrl: String;
  i,t,p,x: Integer;
begin
  if pos('<link',text) > 0 then
    begin
      t := posex('href=', Text, pos('<link',text)+5)+6;
      x := posex('>', Text, pos('<link',text)+5)-1;
      AUrl := copy(Text,t,x-t);
      i := LastDelimiter('/', AUrl);
      Result := Copy(AUrl, i + 1, Length(AUrl) - (i));
    end else result := 'Not Found';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  s: string;
begin
  s := ExtractCSSFileName(memo1.text);
  ShowMessage(s);
end;

Open in new window

Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Ok, this is using "stylesheet" instead of <link as startkey for the search.
I think this is better for your purpose.
function ExtractCSSFileName(Text: String): string;
var
  AUrl: String;
  i,t,p,x: Integer;
begin
  if pos('"stylesheet"',text) > 0 then
    begin
      t := posex('href=', Text, pos('"stylesheet"',text)+12)+6;
      x := posex('>', Text, pos('"stylesheet"',text)+12)-1;
      AUrl := copy(Text,t,x-t);
      i := LastDelimiter('/', AUrl);
      Result := Copy(AUrl, i + 1, Length(AUrl) - (i));
    end else result := 'Not Found';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  s: string;
begin
  s := ExtractCSSFileName(memo1.text);
  ShowMessage(s);
end;

Open in new window

Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

ST3VOAuthor Commented:
Hmm..that's a good point actually...what about the fact that the filename...or last 3 characters will always be css or .css as the last 4.

Could that help to get it more precise? I cannot use stylesheet as I would have to change it myself it the html didn't have it before hand,,,know what I mean?

Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
SO you want to grab any .css name found in the html?
ST3VOAuthor Commented:
I was thinking any .css found in a <link tag which is after the href="  ??? Make sense?

btw: I get an error: [DCC Error] Unit1.pas(61): E2003 Undeclared identifier: 'posex'  what do I need to add to the uses pls?

Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Add strutils
ST3VOAuthor Commented:
Yeah...got that thanks :o)
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Other way, looking for just '.css'
function ExtractCSSFileName(Text: String): string;
var
  AUrl, temppath: String;
  i, t: Integer;
begin
  if pos('.css', Text) > 0 then
  begin
    i := pos('.css', Text); // found drive
    begin
      t := i;
      while Text[t] <> '"' do
        dec(t);
      temppath := copy(Text, t, i-t+1);
    end;
    AUrl := temppath+'css';
    i := LastDelimiter('/', AUrl);
    Result := copy(AUrl, i + 1, Length(AUrl) - (i));
  end
  else
    Result := 'Not Found';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
  s: string;
begin
  s := ExtractCSSFileName(Memo1.Text);
  ShowMessage(s);
end;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ST3VOAuthor Commented:
Perfect Thanks!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.