Skip to content

Instantly share code, notes, and snippets.

Last active Jun 4, 2018
What would you like to do?
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