All pastes #968517 Raw Edit

Unnamed

public text v1 · immutable
·published 2008-04-03 02:25 UTC
rendered paste body
private function _decodeHeader($input)
    {
        // Remove white space between encoded-words
        $input = preg_replace('/(=?[^?]+?(Q|B)?[^?]*?=)( |' . "t|" . "\r\n" . ')+=?/', '1=?', $input);

        // For each encoded-word...
        while (preg_match('/(=?([^?]+)?(Q|B)?([^?]*)?=)/', $input, $matches)) {

            $encoded  = $matches[1];
            $charset  = $matches[2];
            $encoding = $matches[3];
            $text     = $matches[4];

            switch ($encoding) {
                case 'B':
                    $text = base64_decode($text);
                    break;

                case 'Q':
                    $text = str_replace('_', ' ', $text);
                    preg_match_all('/=([A-F0-9]{2})/', $text, $matches);
                    foreach($matches[1] as $value)
                        $text = str_replace('='.$value, chr(hexdec($value)), $text);
                    break;
            }

            $input = str_replace($encoded, $text, $input);
        }
        
        return $input;
    }