You are here

function ical_quoted_printable_encode in Event 5.2

Same name and namespace in other branches
  1. 5 ical.inc \ical_quoted_printable_encode()

encode text using quoted-printable standard

1 call to ical_quoted_printable_encode()
ical_quoted_printable_escaped in ./ical.inc
escape ical separators in quoted-printable encoded code

File

./ical.inc, line 362
API for event import/export in iCalendar format as outlined in Internet Calendaring and Scheduling Core Object Specification http://www.ietf.org/rfc/rfc2445.txt

Code

function ical_quoted_printable_encode($text, $line_max = 76) {
  $hex = array(
    '0',
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    'A',
    'B',
    'C',
    'D',
    'E',
    'F',
  );
  $lines = preg_split("/(?:\r\n|\r|\n)/", $text);
  $eol = "\r\n";
  $linebreak = "=0D=0A";
  $escape = "=";
  $output = "";
  for ($x = 0; $x < count($lines); $x++) {
    $line = $lines[$x];
    $line_len = strlen($line);
    $newline = "";
    for ($i = 0; $i < $line_len; $i++) {
      $c = substr($line, $i, 1);
      $dec = ord($c);

      // convert space at end of line
      if ($dec == 32 && $i == $line_len - 1) {
        $c = $escape . "20";
      }
      elseif ($dec == 61 || $dec < 32 || $dec > 126) {
        $h2 = floor($dec / 16);
        $h1 = floor($dec % 16);
        $c = $escape . $hex["{$h2}"] . $hex["{$h1}"];
      }

      // see if new output line is needed
      if (strlen($newline) + strlen($c) >= $line_max) {
        $output .= $newline . $escape . $eol;
        $newline = "";
      }
      $newline .= $c;
    }
    $output .= $newline;

    // skip last line feed
    if ($x < count($lines) - 1) {
      $output .= $linebreak;
    }
  }
  return trim($output);
}