Skip to content

Instantly share code, notes, and snippets.

@ryanshoover
Created June 20, 2017 18:18
Show Gist options
  • Select an option

  • Save ryanshoover/e27f3df561629f374e6faf829b75c142 to your computer and use it in GitHub Desktop.

Select an option

Save ryanshoover/e27f3df561629f374e6faf829b75c142 to your computer and use it in GitHub Desktop.
Find un-minified CSS files in a website
<?php
/**
* Find the number of unminified CSS files on a website
* @param string $url The root URL of the website to test
* @param integer $lines_per_file What's the max number of lines a minified CSS file should have?
* @return integer Number of CSS files on a website that aren't minified
*/
function how_many_unminified_css_files( $url, $lines_per_file = 3 )
$unminimized_css_files = 0;
// Get the website's HTML
$html = file_get_contents( $url );
// Find all local css files
preg_match( "/({$url}.*\.css)/gi", $html, $css_files );
// Remove the global match that preg_match returns
array_shift( $css_files );
// Loop through all the local CSS files
// And count how many lines they have
foreach( $css_files as $css_file ) {
$linecount = 0;
// "Open" the CSS file
$handle = fopen($css_file, "r");
// Count the number of lines
while(!feof($handle)){
$line = fgets($handle);
$linecount++;
}
// Close the CSS file
fclose($handle);
// If the CSS file has more lines than we deem appropriate,
// we'll consider it not minified
if ( $linecount > $lines_per_file ) {
$unminimized_css_files++;
}
}
// Return the number of files that we don't think are minified
return $unminimized_css_files;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment