Skip to content

Instantly share code, notes, and snippets.

@thijzert thijzert/encoded-word.php
Last active Nov 9, 2017

Embed
What would you like to do?
MIME encoded-word (RFC2047) decoder
<?php
/**
* @param string $value Raw MIME header field
* @return string UTF-8 subject
**/
function decode_header_field( $value )
{
if ( preg_match( "/^\\=\\?([a-z0-9-]+)\\?([QB])\\?(.*)\\?\\=\$/i", $value, $sub ) )
{
$charset = strtoupper($sub[1]);
$encoding = strtoupper($sub[2]);
$raw = $sub[3];
if ( $encoding == "B" )
{
$raw = base64_decode($raw);
}
elseif ( $encoding == "Q" )
{
$hexx = function( $h )
{
return hex2bin(substr($h[0],1));
};
$raw = preg_replace_callback( "/\\=[0-9a-f]{2}/i", $hexx, $raw );
}
else
{
return $value;
}
if ( $charset != "UTF-8" )
{
$raw = iconv( $charset, "UTF-8//IGNORE", $raw );
}
return $raw;
}
return $value;
}
$test_cases = [
'=?utf-8?B?UmU6IENoYWluJndpcmUgcm9wZSBhY2...?=',
'=?GB2312?B?UmU6TWFudWZhY3R1cmVyIGxvb2sgZ...?=',
'=?utf-8?B?UmU6QW5pbWFsIGVhciB0YWcgYW5kIG...?=',
'PCB supplier in China- RF-4, rogers, arl...',
'=?utf-8?B?UmU6IEFtbW9uaXVtIFBvbHlwaG9zcG...?=',
'the best price of glyphosate 41% SL From...',
'=?utf-8?B?UmU6U2V2ZXIgaGlnaCBxdWFsaXR5IE...?=',
'=?iso-8859-1?Q?=A1Hola,_se=F1or!?=',
];
foreach ( $test_cases as $tc )
{
print( "{$tc}\n " . decode_header_field( $tc ) . "\n" );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.