3

I am building my custom library for merging all screen css stylesheets but I am not sure how to get stylesheets for the media type screen only. For example:

<!-- This should be fetched -->
<link href="http://www.domain.com/style.css" rel="stylesheet" type="text/css" />
<!-- This should be fetched -->
<link href="http://www.domain.com/ie.css" rel="stylesheet" type="text/css" />

<style type="text/css" media="all">
  <!-- This should be fetched -->
  @import url("http://static.php.net/www.php.net/styles/phpnet.css");
</style>

<style type="text/css" media="screen">
   <!-- This should be fetched -->
  @import url("http://static.php.net/www.php.net/styles/site.css");
</style>

<style type="text/css" media="print">
  <!-- This should NOT be fetched since it is media type print -->
  @import url("http://static.php.net/www.php.net/styles/print.css");
</style>

Given above string, I just want to extract href and url values. I don't know how to go about with that. Although I did try:

preg_match_all("/(url\([\'\"]?)([^\"\'\)]+)([\"\']?\))/", $html, $matches);
print_r($matches);

But it doesn't return it.

Any solution with php dom, xpath or regex to achieve that ?

1
  • you will want to parse the dom. this might make it easier for you: code.google.com/p/ganon (disclaimer: I've never used it myself, but looks like it will support what you need) Commented Jan 11, 2013 at 6:05

1 Answer 1

5

Here is the working code ! I have created a codepad pastebin also for you: http://codepad.org/WQzcO3k3

<?php

$inputString = '<!-- This should be fetched -->
<link href="http://www.domain.com/style.css" rel="stylesheet" type="text/css" />
<!-- This should be fetched -->
<link href="http://www.domain.com/ie.css" rel="stylesheet" type="text/css" />

<style type="text/css" media="all">
  <!-- This should be fetched -->
  @import url("http://static.php.net/www.php.net/styles/phpnet.css");
</style>

<style type="text/css" media="screen">
   <!-- This should be fetched -->
  @import url("http://static.php.net/www.php.net/styles/site.css");
</style>

<style type="text/css" media="print">
  <!-- This should NOT be fetched since it is media type print -->
  @import url("http://static.php.net/www.php.net/styles/print.css");
</style>';
$outputUrls = array();

@$doc = new DOMDocument();
@$doc->loadHTML($inputString);
$xml = simplexml_import_dom($doc); // just to make xpath more simple

$linksOrStyles = $xml->xpath('//*[@rel="stylesheet" or @media="all" or @media="screen"]');     


//print_r($linksOrStyles);

foreach ($linksOrStyles as $linkOrStyleSimpleXMLElementObj)
{
    if ($linkOrStyleSimpleXMLElementObj->xpath('@href') != false) {
      $outputUrls[] = $linkOrStyleSimpleXMLElementObj['href'] . '';
    } else {
        //get the 'url' value.
        $httpStart = strpos($linkOrStyleSimpleXMLElementObj.'', 'http://');
        $httpEnd = strpos($linkOrStyleSimpleXMLElementObj.'', '"', $httpStart);
        $outputUrls[] = substr($linkOrStyleSimpleXMLElementObj.'', $httpStart, ($httpEnd - $httpStart));
        //NOTE:Use preg_match only to get URL. i had to use strpos here 
        //since codepad.org doesnt suport preg
        /*
        preg_match(
            "#((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)#ie",
            ' ' . $linkOrStyleSimpleXMLElementObj,
            $matches
        );
        print_r($matches);
        $outputUrls[] = $matches[0];
        */
    }
}

echo 'Output Url list: ';
print_r($outputUrls);

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Dev01 for accceptance

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.