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?
 
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

0
 
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
0
 
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

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

0
 
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?

0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
SO you want to grab any .css name found in the html?
0
 
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?

0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Add strutils
0
 
ST3VOAuthor Commented:
Yeah...got that thanks :o)
0
 
ST3VOAuthor Commented:
Perfect Thanks!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.