Example #1
0
 /**
  * Parses a RFC 2616 Media Type and returns its parts in an associative array.
  *
  * media-type = type "/" subtype *( ";" parameter)
  *
  * The media type "text/html; charset=UTF-8" would be parsed and returned with
  * the following array keys and values:
  *
  * "type" => the type as a string, "text"
  * "subtype" => the subtype as a string, "html"
  * "parameters" => an array of parameter names and values, array("charset" => "UTF-8")
  *
  * @param string $rawMediaType The raw media type, for example "application/json; charset=UTF-8"
  * @return array An associative array with parsed information
  */
 public static function parseMediaType($rawMediaType)
 {
     preg_match(self::PATTERN_SPLITMEDIATYPE, $rawMediaType, $matches);
     $result = array();
     $result['type'] = isset($matches['type']) ? $matches['type'] : '';
     $result['subtype'] = isset($matches['subtype']) ? $matches['subtype'] : '';
     $result['parameters'] = array();
     if (isset($matches['parameters'])) {
         foreach (Arrays::trimExplode(';', $matches['parameters']) as $parameter) {
             $pieces = explode('=', $parameter);
             if (count($pieces) === 2) {
                 $name = trim($pieces[0]);
                 $result['parameters'][$name] = trim($pieces[1]);
             }
         }
     }
     return $result;
 }