Split up as CSS string by @media queries.
Paramètres
string $css: String of CSS.
string $starting_string: What to look for when starting to parse the string.
Return value
array array of css with only media queries.
Voir aussi
http://stackoverflow.com/a/14145856/125684
2 calls to advagg_parse_media_blocks()
- advagg_get_css_aggregate_contents dans ./
advagg.missing.inc - Given a list of files, grab their contents and glue it into one big string.
- advagg_split_css_file dans ./
advagg.inc - Given a file info array it will split the file up.
Fichier
-
./
advagg.missing.inc, line 1555
Code
function advagg_parse_media_blocks($css, $starting_string = '@media') {
$media_blocks = array();
$start = 0;
$last_start = 0;
// Using the string as an array throughout this function.
// http://php.net/types.string#language.types.string.substr
while (($start = strpos($css, $starting_string, $start)) !== FALSE) {
// Stack to manage brackets.
$s = array();
// Get the first opening bracket.
$i = strpos($css, "{", $start);
// If $i is false, then there is probably a css syntax error.
if ($i === FALSE) {
continue;
}
// Push bracket onto stack.
array_push($s, $css[$i]);
// Move past first bracket.
++$i;
// Find the closing bracket for the @media statement. But ensure we don't
// overflow if there's an error.
while (!empty($s) && isset($css[$i])) {
// If the character is an opening bracket, push it onto the stack,
// otherwise pop the stack.
if ($css[$i] === "{") {
array_push($s, "{");
}
elseif ($css[$i] === "}") {
array_pop($s);
}
++$i;
}
// Get CSS before @media and store it.
if ($last_start != $start) {
$insert = trim(substr($css, $last_start, $start - $last_start));
if (!empty($insert)) {
$media_blocks[] = $insert;
}
}
// Cut @media block out of the css and store.
$media_blocks[] = trim(substr($css, $start, $i - $start));
// Set the new $start to the end of the block.
$start = $i;
$last_start = $start;
}
// Add in any remaining css rules after the last @media statement.
if (strlen($css) > $last_start) {
$insert = trim(substr($css, $last_start));
if (!empty($insert)) {
$media_blocks[] = $insert;
}
}
return $media_blocks;
}