Extracting linked CSS files with PHP
Whats the 开发者_如何学运维best way to parse HTML and extract linked CSS files using PHP?
DOMDocument
can probably help you out:
$dom = new DOMDocument();
$dom->loadHTMLFile('file.html'); // Can replace with $dom->loadHTML($str);
$link_tags = $dom->getElementsByTagName('link');
foreach($link_tags as $link_tag)
{
// if $link_tag rel == stylesheet
// get href value and load CSS
}
Here is a simple solution using regular expressions.
$content = '...';
$n = preg_match_all('/"([^"]+?\.css)"/', $content, $matches);
if ($n !== FALSE && $n > 0) {
var_dump($matches[1]);
}
I would use curl class to get the HTML file, then use the DOMDocument class to parse the HTML for CSS links. If you are looking for more, you will have to be more specific.
DOMDocument http://it.php.net/domdocument
check in php documentation
精彩评论