Skip to content

Instantly share code, notes, and snippets.

@SocalNick
Created March 27, 2012 16:50
Show Gist options
  • Save SocalNick/2217850 to your computer and use it in GitHub Desktop.
Save SocalNick/2217850 to your computer and use it in GitHub Desktop.
Symfony's Cache-Control header parser
<?php
function parseCacheControl($header)
{
$cacheControl = array();
preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$cacheControl[strtolower($match[1])] = isset($match[2]) && $match[2] ? $match[2] : (isset($match[3]) ? $match[3] : true);
}
return $cacheControl;
}
var_dump(parseCacheControl('no-store& private=test, no-cache="a,b,c"'));
$start = microtime(true);
for ($i=0; $i<10000; $i++) {
parseCacheControl('no-store, private=test, no-cache="a,b,c"');
}
var_dump(microtime(true) - $start);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment