You are here

function parse_media_blocks in CSS Embedded Images 7

Split up as CSS string by @media queries.

Return value

array of css with only media queries.

See also

http://stackoverflow.com/questions/14145620/regular-expression-for-media...

2 calls to parse_media_blocks()
_css_emimage_process in ./css_emimage.module
Helper function to replace URLs with data URIs.
_css_emimage_text_processor in ./css_emimage.advagg.inc
Process the css text and replace it with image data where necessary.

File

./css_emimage.inc, line 171
CSS Embedded Images module.

Code

function parse_media_blocks($css) {
  $media_blocks = array();
  $start = 0;
  while (($start = strpos($css, "@media", $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) {

      // push bracket onto stack
      array_push($s, $css[$i]);

      // move past first bracket
      $i++;
      while (!empty($s)) {

        // 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++;
      }

      // cut the media block out of the css and store
      $media_blocks[] = substr($css, $start, $i + 1 - $start);

      // set the new $start to the end of the block
      $start = $i;
    }
  }
  return $media_blocks;
}