quoted-printable文字列の変換
quoted-printable文字列とは、メールなどに使われるコードで、
RFC2045のsection6.7やRFC2821の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進数 | 
|---|---|---|
| (スペース) | 32 | 0x20 | 
| ! | 33 | 0x21 | 
| " | 34 | 0x22 | 
| # | 35 | 0x23 | 
| $ | 36 | 0x24 | 
| % | 37 | 0x25 | 
| & | 38 | 0x26 | 
| ' | 39 | 0x27 | 
| ( | 40 | 0x28 | 
| ) | 41 | 0x29 | 
| * | 42 | 0x2a | 
| + | 43 | 0x2b | 
| , | 44 | 0x2c | 
| - | 45 | 0x2d | 
| . | 46 | 0x2e | 
| / | 47 | 0x2f | 
| 0 | 48 | 0x30 | 
| 1 | 49 | 0x31 | 
| 2 | 50 | 0x32 | 
| 3 | 51 | 0x33 | 
| 4 | 52 | 0x34 | 
| 5 | 53 | 0x35 | 
| 6 | 54 | 0x36 | 
| 7 | 55 | 0x37 | 
| 8 | 56 | 0x38 | 
| 9 | 57 | 0x39 | 
| : | 58 | 0x3a | 
| ; | 59 | 0x3b | 
| < | 60 | 0x3c | 
| > | 62 | 0x3e | 
| ? | 63 | 0x3f | 
| @ | 64 | 0x40 | 
| A | 65 | 0x41 | 
| B | 66 | 0x42 | 
| C | 67 | 0x43 | 
| D | 68 | 0x44 | 
| E | 69 | 0x45 | 
| F | 70 | 0x46 | 
| G | 71 | 0x47 | 
| H | 72 | 0x48 | 
| I | 73 | 0x49 | 
| J | 74 | 0x4a | 
| K | 75 | 0x4b | 
| L | 76 | 0x4c | 
| M | 77 | 0x4d | 
| N | 78 | 0x4e | 
| O | 79 | 0x4f | 
| P | 80 | 0x50 | 
| Q | 81 | 0x51 | 
| R | 82 | 0x52 | 
| S | 83 | 0x53 | 
| T | 84 | 0x54 | 
| U | 85 | 0x55 | 
| V | 86 | 0x56 | 
| W | 87 | 0x57 | 
| X | 88 | 0x58 | 
| Y | 89 | 0x59 | 
| Z | 90 | 0x5a | 
| [ | 91 | 0x5b | 
| \ | 92 | 0x5c | 
| ] | 93 | 0x5d | 
| ^ | 94 | 0x5e | 
| _ | 95 | 0x5f | 
| ` | 96 | 0x60 | 
| a | 97 | 0x61 | 
| b | 98 | 0x62 | 
| c | 99 | 0x63 | 
| d | 100 | 0x64 | 
| e | 101 | 0x65 | 
| f | 102 | 0x66 | 
| g | 103 | 0x67 | 
| h | 104 | 0x68 | 
| i | 105 | 0x69 | 
| j | 106 | 0x6a | 
| k | 107 | 0x6b | 
| l | 108 | 0x6c | 
| m | 109 | 0x6d | 
| n | 110 | 0x6e | 
| o | 111 | 0x6f | 
| p | 112 | 0x70 | 
| q | 113 | 0x71 | 
| r | 114 | 0x72 | 
| s | 115 | 0x73 | 
| t | 116 | 0x74 | 
| u | 117 | 0x75 | 
| v | 118 | 0x76 | 
| w | 119 | 0x77 | 
| x | 120 | 0x78 | 
| y | 121 | 0x79 | 
| z | 122 | 0x7a | 
| { | 123 | 0x7b | 
| | | 124 | 0x7c | 
| } | 125 | 0x7d | 
| ~ | 126 | 0x7e | 
関連記事
- リクエストヘッダーやリクエストボディーなどを取得する方法
 - 負荷が高いときには503エラーを返す方法
 - サイトの更新情報をPINGサーバに送信する方法
 - PHPでロードアベレージを表示させる方法
 - PHPでTwitterのツイートをする/ツイート一覧を取得する/検索する(API v1.1)
 - インクルードパスを設定する方法
 - オブジェクト(Object)を配列(Array)に変換する方法
 - PHPでgzip圧縮形式(gz圧縮)のファイルを読み書きする方法
 - PHPでfacebook投稿時に公開範囲を指定する方法
 - PHPで複数の画像をfacebookに投稿する方法
 - PHPでfacebookのフィード(ウォール)に投稿する方法
 - PHPでのfacebookアプリの認証処理(APIを使うユーザー認証)
 - MySQL関数のまとめ
 - MySQLサーバに接続できるかどうかを確認する
 - ディレクトリ内のファイルのパーミッションを一括で変更する
 - POSTでアップロードできるファイルサイズの制限を変更する方法
 - 暗号化・複合化を行う ブロック暗号
 - date型やdatetime型と年月日時分秒への変換
 - 関数・メソッドの存在を調べる方法
 - PHPでTwitterのbotを作る方法 ツイートをする/ツイート一覧を取得する(API v1)
 - strtotimeの指定
 - PHPでHTMLメールを送る方法
 - ディレクトリセパレータを短く定義する DIRECTORY_SEPARATOR
 - モザイク画像を作る方法
 - HTML内のアクセス解析タグを除去する方法
 - HTMLのTABLEタグを簡単にCSVファイルに変換する方法
 - 画像表示のときに指定サイズにリサイズする(画像の拡大縮小)
 - テキストを可逆的な暗号化する Crypt_Blowfish
 - PHPでwhois検索をする Net_Whois
 - よく使うヘッダー関数のまとめ
 - キャリア・世代を判別する
 - mb_send_mailでCCやBCCを指定する 表示名を指定する
 - より高速に、推測困難な一意なIDを生成する方法
 - 画像ファイルを指定容量ぎりぎりに圧縮する
 - 指定したHTTPヘッダーが送信済みあるいは送信予定に含まれているか
 - DOCUMENT ROOTを得る $_SERVER["DOCUMENT_ROOT"]は使えない!
 - マルチバイト文字列(日本語文字)を一文字づつ取り出す
 - ファイルを削除する/フォルダを削除する
 - ファイルを読み込む/ファイルに書き込む
 - ディレクトリ内のファイル一覧を取得する
 
スポンサーリンク





