0

I have a script which is suppose to collect all the css form a defined url or page. I have tried everything and for some reason it will not get it to detect linked stylesheets such as

<link rel="stylesheet" href="css/typography.css" /> 

I have tried everything I can think of. This is the code I am using that collects on page css and imports. Any help adding the link system would be great.

function scan($page_content){
    $i = 0;
    if(ereg("<style( *[\n]*.*)>\n*(.\n*)*<\/style>", $page_content)){
        if(preg_match_all("/(@\s*import\s* (url((\"|')?)?((\"|')?)|(\"|'){1}).+(\"|')?\)?)/", $page_content, $ext_stylesheets)){
            foreach($ext_stylesheets[0] as $stylesheet){
                $css_content[$i] = preg_replace("/(@\s*import\s*)|(url\(?((\"|')?))|(\"|'){1}|\)?(\"|')?;|(\s)/", "", $stylesheet);
                $i++;
            }
            $array = 1;
        }
        $inline_notused = $this->check_file($page_content, $page_content);
    }
    else die("No page styles, sorry!".$this->helptext);
}
4
  • 8
    Don't use regex for HTML... use a DOM parser. Commented Oct 11, 2012 at 19:29
  • 1
    I believe you could use xPath to parse the DOM rather than regex? Commented Oct 11, 2012 at 19:50
  • 1
    And if you must use regex, don't use ereg. it's deprecated and will be removed from PHP at some point. Commented Oct 12, 2012 at 20:49
  • What do you want the result to be with linked stylesheets? Do you want them to be @imported, or do you want the script to get the contents of the style? Commented Oct 14, 2012 at 5:50

1 Answer 1

1

Here's a nice DOM/XPath way (demo):

function scan($html) {
    $dom = new DOMDocument;
    $dom->loadHTML($html);
    $path = new DOMXPath($dom);
    $nodes = $path->query('//style|//link');
    $style = '';
    foreach($nodes as $node) {
        if($node->tagName === 'style') {
            $style .= $node->nodeValue;
        } elseif($node->tagName === 'link') {
            $style .= "@import url('{$node->getAttribute('href')}')";
        } else {
            // Invalid
        }
        $style .= PHP_EOL;
    }
    return $style;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Kinda looks like you need to parse the <style> elements for imports and remove them, rather than including the whole content.
@cHao Ah, I just looked at the code more closely, and it appears that he wants to get every stylesheet URL.

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.