public static function parse($headers) {
       $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers));

       if(empty($fields)) return [];

       return array_reduce($fields, function($carry, $field) {
           $match = [];
           if (!preg_match('/([^:]+): (.+)/m', $field, $match)) return $carry;

           $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./',function($matches) {
               return strtoupper($matches[0]);
           }, strtolower(trim($match[1])));

           if (!isset($carry[$match[1]])) {
               $carry[$match[1]] = trim($match[2]);
               return $carry;
           }
           
           if (is_array($carry[$match[1]])) {
               $carry[$match[1]][] = trim($match[2]);      
               return $carry;
           }
           
           $carry[$match[1]] = [$carry[$match[1]], trim($match[2])];

           return $carry;
       }, []);
    }
public static function parse($headers) {
       $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers));

       if(empty($fields)) return [];

       return array_reduce($fields, function($carry, $field) {
           $match = [];
           if (!preg_match('/([^:]+): (.+)/m', $field, $match)) return $carry;

           $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./',function($matches) {
               return strtoupper($matches[0]);
           }, strtolower(trim($match[1])));

           if (!isset($carry[$match[1]])) {
               $carry[$match[1]] = trim($match[2]);
               return $carry;
           }
           
           if (is_array($carry[$match[1]])) {
               $carry[$match[1]][] = trim($match[2]);      
               return $carry;
           }
           
           $carry[$match[1]] = [$carry[$match[1]], trim($match[2])];

           return $carry;
       }, []);
    }
public static function parse ( $ headers ) { $ fields = explode ( "\r\n" , preg_replace ( '/\x0D\x0A[\x09\x20]+/' , ' ' , $ headers ) ) ; if ( empty ( $ fields ) ) return [ ] ; return array_reduce ( $ fields , function ( $ carry , $ field ) { $ match = [ ] ; if ( ! preg_match ( '/([^:]+): (.+)/m' , $ field , $ match ) ) return $ carry ; $ match [ 1 ] = preg_replace_callback ( '/(?<=^|[\x09\x20\x2D])./' , function ( $ matches ) { return strtoupper ( $ matches [ 0 ] ) ; } , strtolower ( trim ( $ match [ 1 ] ) ) ) ; if ( ! isset ( $ carry [ $ match [ 1 ] ] ) ) { $ carry [ $ match [ 1 ] ] = trim ( $ match [ 2 ] ) ; return $ carry ; } if ( is_array ( $ carry [ $ match [ 1 ] ] ) ) { $ carry [ $ match [ 1 ] ] [ ] = trim ( $ match [ 2 ] ) ; return $ carry ; } $ carry [ $ match [ 1 ] ] = [ $ carry [ $ match [ 1 ] ] , trim ( $ match [ 2 ] ) ] ; return $ carry ; } , [ ] ) ; }
Parse Http Headers in array

@param  string $headers
@return array
Parse Http Headers in array