Link to home
Start Free TrialLog in
Avatar of ST3VO
ST3VOFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of nordtorp
nordtorp
Flag of Norway image

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
Avatar of Ferruccio Accalai
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

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

Avatar of ST3VO

ASKER

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?

SO you want to grab any .css name found in the html?
Avatar of ST3VO

ASKER

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?

Add strutils
Avatar of ST3VO

ASKER

Yeah...got that thanks :o)
ASKER CERTIFIED SOLUTION
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ST3VO

ASKER

Perfect Thanks!!!