home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / admin / qp_enc.inc < prev    next >
Encoding:
Text File  |  2003-03-16  |  1.4 KB  |  48 lines

  1. <?php
  2. /////////////////////////////
  3. //    include/qp_enc.inc
  4. //        
  5. //        Author: pob@medienrecht.org
  6. //        Source: http://www.php.net/manual/en/function.quoted-printable-decode.php
  7. //        Description:
  8. //                quoted printable encoding function
  9. //        License: 
  10. //                Public domain?
  11. //                Used with implicit permission
  12. //
  13. ////////////////////////////
  14.  
  15.  
  16. function qp_enc($input = "quoted-printable encoding test string", $line_max = 76) {
  17.  
  18.     $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  19.     $lines = preg_split("/(?:\r\n|\r|\n)/", $input);
  20.     $eol = "\r\n";
  21.     $escape = "=";
  22.     $output = "";
  23.  
  24.     while( list(, $line) = each($lines) ) {
  25.         //$line = rtrim($line); // remove trailing white space -> no =20\r\nnecessary
  26.         $linlen = strlen($line);
  27.         $newline = "";
  28.         for($i = 0; $i < $linlen; $i++) {
  29.             $c = substr($line, $i, 1);
  30.             $dec = ord($c);
  31.             if ( ($dec == 32) && ($i == ($linlen - 1)) ) { // convert space at eol only
  32.                 $c = "=20"; 
  33.             } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { //always encode "\t", which is *not* required
  34.                 $h2 = floor($dec/16); $h1 = floor($dec%16); 
  35.                 $c = $escape.$hex["$h2"].$hex["$h1"]; 
  36.             }
  37.             if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is notcounted
  38.                 $output .= $newline.$escape.$eol; // soft line break; "=\r\n" is okay
  39.                 $newline = "";
  40.             }
  41.             $newline .= $c;
  42.         } // end of for
  43.         $output .= $newline.$eol;
  44.     }
  45.     return trim($output);
  46.  
  47. }
  48. ?>