function calendar_ics_split

6.x calendar.module calendar_ics_split($preamble = "DESCRIPTION:", $value)

This lets us split up a line for ics into 75-char octets, according to the rules from: https://icalendar.org/iCalendar-RFC-5545/3-1-content-lines.html

This function comes from: https://gist.github.com/hugowetterberg/81747

1 call to calendar_ics_split()
calendar_get_ics_invitation_string in modules/calendar/calendar.module
Return back an ics file (as a string) to be used as an attachment for emails, which will facilitate a calendar invitation.

File

modules/calendar/calendar.module, line 3523

Code

function calendar_ics_split($preamble = "DESCRIPTION:", $value) {
  $value = trim($value);
  $value = strip_tags($value);
  $value = preg_replace('/\n+/', ' ', $value);
  $value = preg_replace('/\s{2,}/', ' ', $value);

  $preamble_len = strlen($preamble);

  $lines = array();
  while (strlen($value) > (75 -$preamble_len)) {
    $space = (75 -$preamble_len);
    $mbcc = $space;
    while ($mbcc) {
      $line = mb_substr($value, 0, $mbcc);
      $oct = strlen($line);
      if ($oct > $space) {
        $mbcc -= $oct -$space;
      }
      else {
        $lines [] = $line;
        $preamble_len = 1; // Still take the tab into account
        $value = mb_substr($value, $mbcc);
        break;
      }
    }
  }
  if (!empty($value)) {
    $lines [] = $value;
  }

  return join("\n\t", $lines);
}