1

As the title suggest i am trying to find all CSS files on a website (for later use i will find all image urls in each of the CSS files on the server).

Now ive tried the following:

    $url_to_test = $_GET['url'];
$file = file_get_contents($url_to_test);
$doc = new DOMDocument();
$doc->loadHTML($file);
$domcss = $doc->getElementsByTagName('css');

However the array domcss turned out empty (for a site i know has alot of css files).

So my question is how do i find all css files loaded on a given page?

4
  • The html tag for css ist link Commented Nov 18, 2013 at 9:42
  • Uhm, im not familiar with things like that, but the "TagName" is not css. You need <link rel="stylesheet" href="your/css/file">, so ofc you get an enmty array. Commented Nov 18, 2013 at 9:42
  • May I ask what your use case is? It sounds like there may be a simpler solution to the problem. Commented Nov 18, 2013 at 9:46
  • I have a javascript that calls a function that quickly will get all images of the site and returns it as a json Commented Nov 18, 2013 at 9:56

2 Answers 2

5

you should check for link not css, change:

$domcss = $doc->getElementsByTagName('css');

to

$domcss = $doc->getElementsByTagName('link');
foreach($domcss as $links) {
    if( strtolower($links->getAttribute('rel')) == "stylesheet" ) {
       echo "This is:". $links->getAttribute('href') ."<br />";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! how will i check that the link is of type css?
2

Try this:

preg_match('/<link rel="stylesheet" href="(.*?)" type="text\/css">/',$data,$output_array);

6 Comments

what is data and what is output?
data is your data you grabbed, with curl or any other way. And output is an array of css files..
Okay. The following returns an empty array: $file = file_get_contents('as.com/'); preg_match('/<link rel="stylesheet" href="(.*?)" type="text\/css">/', $file, $matches);
Okay apprently it cant type http but it is http as.com
try to grab data with curl
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.