/i', "\n\n", $str); $str = preg_replace('/\/i', "\n", $str); $str = strip_tags($str); $str = trim(html_entity_decode($str, ENT_QUOTES, 'UTF-8')); // no more than two newlines please $str = preg_replace("/(\r?\n){3,}/", "\n\n", $str); // lines can't be more than 75 chars, use 60 to be safe $lines = str_split($str, 60); foreach ($lines as $key => $line) { // escape special icalendar chars and convert newlines to '\n' $lines[$key] = str_replace(array('\\', ',', ';'), array('\\\\', '\,', '\;'), $lines[$key]); $lines[$key] = preg_replace("/\r?\n/", '\n', $lines[$key]); } return implode("\r\n ", $lines); } $location = << 201 East Gregory Street
Pensacola, FL 32502 EOT; $location = str_replace("\n", '', $location); $content = "BEGIN:VCALENDAR\n". "VERSION:2.0\n". "PRODID:-//DonaldJTrumpForPresident//NONSGML v1.0//EN\n". "BEGIN:VEVENT\n". "UID:".md5(uniqid(mt_rand(), true))."@donaldjtrump.com\n". "DTSTAMP:20171208T190000\n". "DTSTART:20171208T190000\n". "DTEND:20171208T190000\n". "SUMMARY:".cleanEscape("Donald J. Trump for President in Pensacola, Florida")."\n". "LOCATION:".cleanEscape($location)."\n". "END:VEVENT\n". "END:VCALENDAR"; $filename = 'Event-pensacola-fl-dec-2017.ics'; // Set the headers header('Content-type: text/calendar; charset=utf-8'); header('Content-Disposition: attachment; filename=' . $filename); // Dump load echo $content; ?>