title: How can I parse $_SERVER['HTTP_COOKIE'] in pure PHP?
body: <p>I need to parse $_SERVER['HTTP_COOKIE'] string in pure PHP. I have no access to $_COOKIE or functions like http_parse_cookie (PECL).</p>
  
  <p>I tried with a function like:</p>
  
  <pre><code>function ParseCookies($strHeaders)
  {
  $result = array();              
      $aPairs = explode(';', $strHeaders);
      foreach ($aPairs as $pair)
      {
          $aKeyValues = explode('=', trim($pair), 2);
          if (count($aKeyValues) == 2)
          {
              switch ($aKeyValues[0])
              {
                  case 'path':
                  case 'domain':
                      $aTmp[trim($aKeyValues[0])] = urldecode(trim($aKeyValues[1]));
                      break;
                  case 'expires':
                      $aTmp[trim($aKeyValues[0])] = strtotime(urldecode(trim($aKeyValues[1])));
                      break;
                  default:
                      $aTmp['name'] = trim($aKeyValues[0]);
                      $aTmp['value'] = trim($aKeyValues[1]);
                      break;
              }
          }
          $result[] = $aTmp;
      }
  return $result;
  }
  </code></pre>
  
  <p>but the explode pattern is trivial, because I can have a cookie like  <code>name="a;b;c=1;";</code> </p>
  
  <p>I need something to tokenize the string in the proper way. Any ideas?</p>