PHP & Regex, replacing multiple lines of text in a .ics feed
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I have a Google Calendar ICS feed that needs to go into a publicly shared calendar. However, I don't want the details of the events to be visible, I only want to show that time is "reserved".

In order to do this, I'm getting the contents of the ICS feed and trying to replace certain values of some lines with RegEx.

There are 2 types of content in the ICS file I want to replace. On any line that begins with "SUMMARY:", I want to replace the summary text with "Private Event".

I also need to replace the text for the "DESCRIPTION:" line in the ICS file.. however the description field is frequently multiple lines of text.

The following code works for the SUMMARY Field, but I'm not sure how to change the RegEx pattern for the DESCRIPTION field in order to get the multiple lines..

`
$source_ics = file_get_contents('https://calendar.google.com/calendar/ical/events%40steeplesquare.com/public/basic.ics');

$pattern='/SUMMARY:(.*)$/im';
$pattern2='/DESCRIPTION:(.*)$/im';
$new_ics = preg_replace($pattern, 'SUMMARY:Private Event'.Chr(13), $source_ics);
$new_ics = preg_replace($pattern2, 'DESCRIPTION:Private Event'.Chr(13), $new_ics);

echo str_replace(Chr(13).Chr(10),"<br />",$new_ics);`
awarded to alexanderk23

Crowdsource coding tasks.

1 Solution

Winning solution

$source_ics = file_get_contents('https://calendar.google.com/calendar/ical/events%40steeplesquare.com/public/basic.ics');
$pattern = "/(SUMMARY|DESCRIPTION):(.*?)(TRANSP|LAST-MODIFIED):/ims";
$new_ics = preg_replace($pattern, "$1:Private Event\n$3:", $source_ics);
echo $new_ics;