regex to extract css links
Moderator: General Moderators
-
klevis miho
- Forum Contributor
- Posts: 413
- Joined: Wed Oct 29, 2008 2:59 pm
- Location: Albania
- Contact:
regex to extract css links
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
Now I have done this:
'#type="text/css" rel="stylesheet" href="(.+?)"#s'
but this isn't so good
Re: regex to extract css links
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
Thank you Benjamin, but does it work if:
$line = '<link type="text/css" media="all" href="css/style.css" rel="stylesheet"/>'; ?
$line = '<link type="text/css" media="all" href="css/style.css" rel="stylesheet"/>'; ?
Re: regex to extract css links
How about you try it and see 
-
klevis miho
- Forum Contributor
- Posts: 413
- Joined: Wed Oct 29, 2008 2:59 pm
- Location: Albania
- Contact:
Re: regex to extract css links
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.
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
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:
-
klevis miho
- Forum Contributor
- Posts: 413
- Joined: Wed Oct 29, 2008 2:59 pm
- Location: Albania
- Contact:
Re: regex to extract css links
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=""
'#<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
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?
'#<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?