我正在构建自定义库以合并所有屏幕css样式表,但我不知道如何仅为媒体类型screen
获取样式表。例如:
<!-- 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>
鉴于上述字符串,我只想提取href
和url
值。我不知道该怎么做。虽然我尝试过:
preg_match_all("/(url\([\'\"]?)([^\"\'\)]+)([\"\']?\))/", $html, $matches);
print_r($matches);
但它没有归还它。
使用php dom,xpath或regex实现的任何解决方案?
答案 0 :(得分:5)
这是工作代码! 我也为您创建了一个键盘缓冲区: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);
?>