Skip to content

Instantly share code, notes, and snippets.

@cemjotform
Last active June 4, 2018 09:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cemjotform/c2161282dc7fd3cdc47ed99a9648e42f to your computer and use it in GitHub Desktop.
Save cemjotform/c2161282dc7fd3cdc47ed99a9648e42f to your computer and use it in GitHub Desktop.
Medium PHP#1
// Define regexes for css include types
$styleTagRegex = '/<style[type="text\/css"]?.*?>(.*?)<\/style>/is';
$linkStyleRegex = '/<link(?=[^<>]*?)(?=[^<>]*?(?:href=["\'](.*?)["\']|>))(?=[^<>]*rel=["\']stylesheet["\'])(?:.*?<\/\1>|[^<>]*>)/i';
// Initialize array of css contents
$cssSource = array('internal' => '');
// Get page content
function requestPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$result = curl_exec($ch);
curl_close ($ch);
return $result;
}
// Fetch html
$htmlSource = requestPage($url);
// Match link and style tags
preg_match_all($linkStyleRegex, $htmlSource, $styleLinks);
preg_match_all($styleTagRegex, $htmlSource, $styleTags);
// Fetch source of styleLinks and append to the list
foreach($styleLinks[1] as $styleLink) {
// Get content of css link
$linkSource = requestPage($styleLink);
preg_match_all($cssImportRegex, $linkSource, $)
$cssSource[$styleLink] = $linkSource;
}
// Merge internal css
foreach($styleTags[1] as $styleTag) {
// Get content of css link
$cssSource['internal'] .= $linkSource;
}
$completeContent = implode('\n', $cssSource);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment