Skip to content

Instantly share code, notes, and snippets.

@erunion
Last active October 7, 2015 02:07
Show Gist options
  • Save erunion/3088095 to your computer and use it in GitHub Desktop.
Save erunion/3088095 to your computer and use it in GitHub Desktop.
PHP function to parse a blob of CSS and count up the total number of selectors present. Useful if you need/want to factor this into your test suite to make sure your CSS files never reach the 4095 limit in IE9 and lower.
<?php
/**
* Parse out a given string containing CSS and count up the total number of
* selectors that are present in it.
*
* @return integer
*/
function countSelectors($css) {
$css = preg_replace('/\{(.*?)\}/s', '', $css);
$css = preg_replace('/[,]/', "\n", $css);
$selectors = explode("\n", $css);
$selectors = array_filter($selectors);
return count($selectors);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment