Page 1 of 1

regex to extract css links

Posted: Wed May 12, 2010 8:55 am
by klevis miho
Yeah can anyone help me with a regex which gets css links.
Now I have done this:
'#type="text/css" rel="stylesheet" href="(.+?)"#s'

but this isn't so good

Re: regex to extract css links

Posted: Wed May 19, 2010 6:37 am
by Benjamin
This may work for you:

Code: Select all

$line = '<link rel="stylesheet" type="text/css" media="all" href="css/style.css" />';

if (preg_match('#<[^>]+text/css[^>]+href\s{0,2}=\s{0,2}["\']{0,1}([^"\']+)["\']{0,1}[^>]+>#', $line, $match)) {
    echo $match[1];
}

Re: regex to extract css links

Posted: Wed May 19, 2010 6:42 am
by klevis miho
Thank you Benjamin, but does it work if:
$line = '<link type="text/css" media="all" href="css/style.css" rel="stylesheet"/>'; ?

Re: regex to extract css links

Posted: Wed May 19, 2010 6:49 am
by Benjamin
How about you try it and see :wink:

Re: regex to extract css links

Posted: Thu May 20, 2010 3:50 am
by klevis miho
Ok I've tried it benjamin and in my test it got 1 out of 10 .css files.
Your regex didn't catch for example this: <link rel="stylesheet" href="/templates/bbtheme/css/styles.css" type="text/css" />

My regex got them all.

Re: regex to extract css links

Posted: Thu May 20, 2010 4:28 am
by Benjamin
Mine only catches them if the type is before the href.

Re: regex to extract css links

Posted: Thu May 20, 2010 4:32 am
by klevis miho
Yeah :)

Re: regex to extract css links

Posted: Tue May 25, 2010 7:30 am
by klevis miho
My regex is like this now:
'#<link[ a-zA-Z0-9="/]+href="(.+?)"#s'

There is a problem with this:
It also catches favicon links and other stuff.

I need to find a way on how to get the href that is inside a link which has the atrribute style="text/css", before or after the href=""

Re: regex to extract css links

Posted: Tue May 25, 2010 8:36 am
by klevis miho
Now I did this:
'#<link[ a-zA-Z0-9="/]+(?:type="text/css")?+[ a-zA-Z0-9="/]+href="(.+?)"#s'

but can anyone explain me what this: (?:type="text/css")? does?