Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

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
0
ST3VO
Asked:
ST3VO
  • 5
  • 4
1 Solution
 
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
 
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
Independent Software Vendors: 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!

 
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
 
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
 
ST3VOAuthor Commented:
Perfect Thanks!!!
0

Featured Post

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.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now