我是新的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;
是否可以使用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;