quoted-printable文字列の変換

quoted-printable文字列とは、メールなどに使われるコードで、 RFC2045のsection6.7RFC2821のsection4.5.2によって定義されています。
基本的な仕様は次の通りです。
[1] 文字コードを10進数で表したときの、32以下、61、126以上の文字の前に『 = 』を付けて16進数の文字コードに置き換える。
[2] 1行が76文字を越す場合([1]の変換を行ってカウント)、『 = 』を付けて改行する。

quoted-printableでエスケープされない文字の一覧
ASCIIコード表

PHPでは、quoted-printable文字列にエンコードする関数quoted_printable_encodeやimap_8bit、デコードする関数quoted_printable_decodeやimap_qprintがあります。
しかしquoted_printable_encodeはPHP5.3.0以上、quoted_printable_decodeはPHP4以上ではないと使えませんし、 imap_8bitやimap_qprintはIMAP モジュールが必要となります。

実はmb_list_encodings()の戻り値にQuoted-Printableがあった場合は、mb_convert_encoding()で変換できます。

このような感じ

mb_convert_encoding($str,"Quoted-Printable","UTF-8");

以下はquoted_printable_encode関数とquoted_printable_decode関数を自作で実装するコードです。

PHPに実装されている関数があればそちらを優先します。

quoted_printable_encode関数

// quoted-printable文字列のエンコード
if (!function_exists('quoted_printable_encode')) {
  if (in_array( "Quoted-Printable", mb_list_encodings())) {
    function quoted_printable_encode( $str )
    {
      return mb_convert_encoding($str,"Quoted-Printable","UTF-8");//ここは変換元のエンコードを書きます。
    }
  }elseif (function_exists('imap_8bit')) {
    function quoted_printable_encode($str)
    {
      return imap_8bit($str);
    }
  }else{
    function quoted_printable_encode($str)
    {
      $crlf="¥r¥n";

      $str=trim($str);

      $lines   = preg_split("/(¥r¥n|¥n|¥r)/s", $str);
      $out     = '';
      $temp = '';
      foreach ($lines as $line)
      {
        for ($j = 0; $j < strlen($line); $j++)
        {
          $char = substr ( $line, $j, 1 );
          $ascii = ord ( $char );

          if ( $ascii < 32 || $ascii == 61 || $ascii > 126 )
          {
            $char = '=' . strtoupper ( dechex( $ascii ) );
          }

          if ( ( strlen ( $temp ) + strlen ( $char ) ) >= 76 )
          {
            $out .= $temp . '=' . $crlf;   $temp = '';
          }
          $temp .= $char;
        }
      }
      $out .= $temp;

      return trim ( $out );
    }
  }
}

quoted_printable_decode関数

// quoted-printable文字列のデコード
if (!function_exists('quoted_printable_decode')) {
  if (in_array( "Quoted-Printable", mb_list_encodings())) {
    function quoted_printable_decode( $str )
    {
      return mb_convert_encoding($str,"UTF-8","Quoted-Printable");//ここは変換元のエンコードを書きます。
    }
  }elseif (function_exists('imap_qprint')) {
    function quoted_printable_decode( $str )
    {
      return imap_qprint($str);
    }
  }else{
    function quoted_printable_decode( $str )
    {
      $out = preg_replace('/=¥r?¥n/', '', $str);
      $out = preg_replace('/=([A-F0-9]{2})/e',  "chr(hexdec ('$1'))" , $out);
      return trim($out);
    }
  }
}

quoted-printableでエスケープされない文字の一覧

文字コードを10進数で表したときの、32以下、61、126以上の文字が『=』でエスケープされます。
61は『=』です。

文字10進数16進数
(スペース)320x20
!330x21
"340x22
#350x23
$360x24
%370x25
&380x26
'390x27
(400x28
)410x29
*420x2a
+430x2b
,440x2c
-450x2d
.460x2e
/470x2f
0480x30
1490x31
2500x32
3510x33
4520x34
5530x35
6540x36
7550x37
8560x38
9570x39
:580x3a
;590x3b
<600x3c
>620x3e
?630x3f
@640x40
A650x41
B660x42
C670x43
D680x44
E690x45
F700x46
G710x47
H720x48
I730x49
J740x4a
K750x4b
L760x4c
M770x4d
N780x4e
O790x4f
P800x50
Q810x51
R820x52
S830x53
T840x54
U850x55
V860x56
W870x57
X880x58
Y890x59
Z900x5a
[910x5b
\920x5c
]930x5d
^940x5e
_950x5f
`960x60
a970x61
b980x62
c990x63
d1000x64
e1010x65
f1020x66
g1030x67
h1040x68
i1050x69
j1060x6a
k1070x6b
l1080x6c
m1090x6d
n1100x6e
o1110x6f
p1120x70
q1130x71
r1140x72
s1150x73
t1160x74
u1170x75
v1180x76
w1190x77
x1200x78
y1210x79
z1220x7a
{1230x7b
|1240x7c
}1250x7d
~1260x7e

関連記事

スポンサーリンク

Date.toDateString

ホームページ製作・web系アプリ系の製作案件募集中です。

上に戻る