0

I`m trying to get all the css code from a website , I have difficuties trying to determine whether the href is realtive or absolute.I was using filter_var() , but it does not properly with urls starting with //.

<?php 
        $file = file_get_contents($url);

        $doc = new DOMDocument();
        $doc->loadHTML($file);

        $domcss = $doc->getElementsByTagName('link');  //Get all .css file
        foreach($domcss as $links) {
            if( strtolower($links->getAttribute('rel')) == "stylesheet" ) {
                $href  =  $links->getAttribute('href');

                if(filter_var($href, FILTER_VALIDATE_URL)){ //Check if href is relative or absolute ,not working correctly
                    $css = file_get_contents($href);
                }else{
                    $css = file_get_contents($this->url.$href);
                }

                //echo $css;

            }
        }

For example:

var_dump(filter_var('//google.bg' , FILTER_VALIDATE_URL)); //false
nikksan
  • 3,341
  • 3
  • 22
  • 27

0 Answers0