我是php的新手,是否有人可以建议如何获取页面样式表的内容,以便它们使用php dom显示在页面上?
让我们说在页首是:
<link rel="stylesheet" href="resources/css/reset.css" type="text/css" media="screen"/>
<link rel="stylesheet" href="resources/css/style.css" type="text/css" media="screen"/>
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css" media="screen"/>
<link rel="stylesheet" href="resources/css/blue.css" type="text/css" media="screen"/>
如何获取这些样式表的内容?
更新
$url = 'http://wordpress.stackexchange.com/questions/60792/replace-image-attributes-for-lazyload-plugin-data-src';
$html = file_get_contents($url);
$xml = new SimpleXMLElement($html);
/* Search for <link rel=stylesheet> */
$result = $xml->xpath('link[@rel="stylesheet"]');
foreach($result as $node) {
echo $node->{'@attributes'}->href . "\n";
}
exit;
答案 0 :(得分:2)
是否可以使用simplexml而不是dom?
http://php.net/manual/en/simplexmlelement.xpath.php
<?php
$url = 'http://wordpress.stackexchange.com/questions/60792/replace-image-attributes-for-lazyload-plugin-data-src';
$url_parts = parse_url($url);
$html = file_get_contents($url);
$doc = new DOMDocument();
// A corrupt HTML string
$doc->loadHTML($html);
$xml = simplexml_import_dom($doc);
/* Search for <link rel=stylesheet> */
$result = $xml->xpath('//link[@rel="stylesheet"]');
foreach($result as $node) {
$link = $node->attributes()->href;
if (substr($link, 0, 4) == 'http') {
// nothing to do
} else if (substr($link, 0, 1) == '/') {
$link = $url_parts['scheme'] . '://' . $link;
} else {
$link = $url_parts['scheme'] . '://' . dirname($url_parts['scheme']) . '/' . $link;
}
echo '--> ' . $link . "\n";
}
exit;