regex to extract css links

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

regex to extract css links

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: regex to extract css links

Post 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];
}
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: regex to extract css links

Post by klevis miho »

Thank you Benjamin, but does it work if:
$line = '<link type="text/css" media="all" href="css/style.css" rel="stylesheet"/>'; ?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: regex to extract css links

Post by Benjamin »

How about you try it and see :wink:
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: regex to extract css links

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: regex to extract css links

Post by Benjamin »

Mine only catches them if the type is before the href.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: regex to extract css links

Post by klevis miho »

Yeah :)
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: regex to extract css links

Post 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=""
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: regex to extract css links

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