URLからhtmlファイルのすべてのCSSファイルを取得しようとしています。
HTMLコードを取得したい場合は、PHP 関数を使用するだけで簡単に取得できますfile_get_contents
。
問題は、 HTMLの URL 内を簡単に検索して、そこから関連するすべてのCSSファイルのファイルまたはコンテンツを取得できるかどうかです。
注- 大量の CSS ファイルを取得するためのエンジンを構築したいので、ソースを読むだけでは十分ではありません..
ありがとう、
HTML 解析にhttp://simplehtmldom.sourceforge.net/を使用してみてください。
require_once 'SimpleHtmlDom/simple_html_dom.php';
$url = 'www.website-to-scan.com';
$website = file_get_html($url);
// You might need to tweak the selector based on the website you are scanning
// Example: some websites don't set the rel attribute
// others might use less instead of css
//
// Some other options:
// link[href] - Any link with a href attribute (might get favicons and other resources but should catch all the css files)
// link[href="*.css*"] - Might miss files that aren't .css extension but return valid css (e.g.: .less, .php, etc)
// link[type="text/css"] - Might miss stylesheets without this attribute set
foreach ($website->find('link[rel="stylesheet"]') as $stylesheet)
{
$stylesheet_url = $stylesheet->href;
// Do something with the URL
}
CSS ファイルを探して HTML タグを解析する必要があります。たとえば、一致する正規表現を探して preg_match を使用して実行できます。
そのようなファイルを見つける正規表現は次のようになります。
\<link .+href="\..+css.+"\>