-1

I am creating an ics file parser on a site I'm working on and no matter what I try to write for checking whether or not the $icsEvent['LOCATION'] index is empty it keeps acting as if it has a value. I am trying to take an address that is entered into an ics event and break it up into location name, location street, location city, location state and location zip. The first event in the ics file has an address where as the second one does not. When it gets to the second event it is still acting as if the second event isn't empty and is causing it to throw an error for an undefined offset.

$file = "https://calendar.google.com/calendar/ical/nhfb76hhlt4ts86jmk4n9bsghk%40group.calendar.google.com/public/basic.ics";
/* Getting events from isc file */
$obj = new ics();
$icsEvents = $obj->getIcsEventsAsArray($file);
/* Here we are getting the timezone to get the event dates according to geo location */
$timeZone = trim($icsEvents[1]['X-WR-TIMEZONE']);
unset($icsEvents[1]);
unset($icsEvents[2]);
unset($icsEvents[3]);
unset($icsEvents[4]);
unset($icsEvents[5]);
unset($icsEvents[6]);
foreach($icsEvents as $icsEvent){
  $eventKey = removeEmail($icsEvent['UID']);
  /* Getting start date and time */
  $start = isset($icsEvent['DTSTART;VALUE=DATE']) ? $icsEvent['DTSTART;VALUE=DATE'] : $icsEvent['DTSTART'];
  /* Converting to datetime and apply the timezone to get proper date time */
  $startDt = new DateTime($start);
  $startDt->setTimezone(new DateTimeZone($timeZone));
  $startDate = $startDt->format('Ymd H:i:s');
  $eventDate = $startDt->format('m.d.Y @ g:iA');
  $startSrch = $startDt->format('Ymd');
  /* Getting end date with time */
  $end = isset($icsEvent['DTEND;VALUE=DATE']) ? $icsEvent['DTEND;VALUE=DATE'] : $icsEvent['DTEND'];
  $endDt = new DateTime($end);
  $endDate = $endDt->format('Ymd H:i:s');
  /* Determining if the event is more than one day */
  if($endDt->format('Ymd') != $startDt->format('Ymd')){
    $eventDays = 1;
  }else{
    $eventDays = 0;
  }
  /* Getting the name of event */
  $eventName = $icsEvent['SUMMARY'];
  /* Getting the description of event */
  $eventDesc = $icsEvent['DESCRIPTION'];
  /* Getting the location of event */
  $eventLoc = $icsEvent['LOCATION'];
  if($eventLoc){
    $parts = explode("\, ",$eventLoc);
    $locName = $parts[0];
    $locStreet = $parts[1];
    $locCity = $parts[2];
    $locStateZ = $parts[3];
    $parts = explode(" ",$locStateZ);
    $locState = $parts[0];
    $navState = convertState($locState);
    $locZIP = $parts[1];
  }else{
    $locName = "Any Location Name";
    $locStreet = "5200 Lake Gold Parkway";
    $locCity = "Anytown";
    $locState = "MN";
    $navState = "Minnesota";
    $locZIP = "80134";
  }
  echo $locName;
  echo $locStreet;
  echo $locCity;
  echo $locState;
  echo $locZIP;
  echo $navState;
  $find = " ";
  $replace = "+";
  $navStreet = str_replace($find,$replace,$locStreet);
  echo $navStreet;
  /* Getting the image of event */
  if(array_key_exists("ATTACH",$icsEvent)){
    $eventImg = imageCorrection($icsEvent['ATTACH']);
  }else{
    $eventImg = "/components/img/event-default";
  }
  /* Creating the url of event */
  $eventURL = seoURL($eventName);
  /* Creating the id of event */
  $startSwtch = date('B',strtotime($startDate));
  $eventID = $eventKey."{$startSwtch}";

  $query = "SELECT * FROM calendar WHERE event_key='{$eventKey}' AND start_date LIKE '{$startSrch}%'";
  $update = "UPDATE calendar SET event='".$connection->real_escape_string($eventName)."',url='".$connection->real_escape_string($eventURL)."',image='".$connection->real_escape_string($eventImg)."',description='".$connection->real_escape_string($eventDesc)."',locationName='".$connection->real_escape_string($locName)."',locationStreet='".$connection->real_escape_string($locStreet)."',locationCity='".$connection->real_escape_string($locCity)."',locationState='".$connection->real_escape_string($locState)."',locationZip='".$connection->real_escape_string($locZIP)."',start_date='{$startDate}',end_date='{$endDate}',multiDay='{$eventDays}',church='1' WHERE (event_key='{$eventKey}' AND start_date LIKE '{$start}%')";
  $insert = "INSERT INTO calendar VALUES ('{$eventKey}','".$connection->real_escape_string($eventName)."','".$connection->real_escape_string($eventURL)."','".$connection->real_escape_string($eventImg)."','".$connection->real_escape_string($eventDesc)."','".$connection->real_escape_string($locName)."','".$connection->real_escape_string($locStreet)."','".$connection->real_escape_string($locCity)."','".$connection->real_escape_string($locState)."','".$connection->real_escape_string($locZIP)."','','','{$startDate}','{$endDate}','{$eventDays}','1','0','0','0','0','0','0','0','0','0','0'";

  if($queryDB = $connection->query($query)){
    $row_cnt = $queryDB->num_rows;
    if($row_cnt != 0){
      $updateDB = $connection->query($update);
    }else{
      $insertDB = $connection->query($insert);
    }
  }

Notice: Undefined offset: 1 in /home/user/mysite.com/components/data/icsparse.php on line 192

Notice: Undefined offset: 2 in /home/user/mysite.com/components/data/icsparse.php on line 193

Notice: Undefined offset: 3 in /home/user/mysite.com/components/data/icsparse.php on line 194

Notice: Undefined offset: 1 in /home/user/mysite.com/components/data/icsparse.php on line 198

ics calendar file Here are the first few entries of the ics file:

BEGIN:VEVENT

DTSTART:20191123T013000Z

DTEND:20191123T023000Z

DTSTAMP:20191122T205609Z

UID:7mg6p2bj83hadlununj7d9ov40@google.com

CREATED:20191122T064508Z

DESCRIPTION:

LAST-MODIFIED:20191122T064508Z

LOCATION:Alaska Pacific University\, 4101 University Dr\, Anchorage\, AK 99 508\, USA

SEQUENCE:0

STATUS:CONFIRMED

SUMMARY:Basketball vs Birchwood Christian

TRANSP:OPAQUE

END:VEVENT

BEGIN:VEVENT

DTSTART;VALUE=DATE:20190624

DTEND;VALUE=DATE:20190629

RRULE:FREQ=YEARLY;WKST=SU;COUNT=1;INTERVAL=1

DTSTAMP:20191122T205609Z

UID:002fe82nhfs3qhfn8i84qe2dm5@google.com

CREATED:20181218T234159Z

DESCRIPTION:

LAST-MODIFIED:20191122T060518Z

LOCATION:

SEQUENCE:1

STATUS:CONFIRMED

SUMMARY:Soccer Camp (PreK-6th)

TRANSP:OPAQUE

END:VEVENT

BEGIN:VEVENT

DTSTART;VALUE=DATE:20190617

DTEND;VALUE=DATE:20190622

RRULE:FREQ=YEARLY;WKST=SU;COUNT=1;INTERVAL=1

DTSTAMP:20191122T205609Z

UID:4e27isqv5puk2ctc2r2pa6vuc9@google.com

CREATED:20181218T234147Z

DESCRIPTION:

LAST-MODIFIED:20191122T060517Z

LOCATION:

SEQUENCE:1

STATUS:CONFIRMED

SUMMARY:STEM Camp (K-8th)

TRANSP:OPAQUE

END:VEVENT

BEGIN:VEVENT

DTSTART:20191124T010000Z

DTEND:20191124T040000Z

DTSTAMP:20191122T205609Z

UID:4gi5mcte11iefak3rhba9l05sk@google.com

CREATED:20191119T232703Z

DESCRIPTION:

LAST-MODIFIED:20191119T232703Z

LOCATION:

SEQUENCE:0

STATUS:CONFIRMED

SUMMARY:Parents' Night Out

TRANSP:OPAQUE

END:VEVENT

Here is what var_export($icsEvent) is returning for the first few entries:

array ( 'BEGIN' => 'VEVENT ', 'DTSTART' => '20191123T013000Z ', 'DTEND' => '20191123T023000Z ', 'DTSTAMP' => '20191124T081354Z ', 'UID' => '7mg6p2bj83hadlununj7d9ov40@google.com ', 'CREATED' => '20191122T064508Z ', 'DESCRIPTION' => ' ', 'LAST-MODIFIED' => '20191122T064508Z ', 'LOCATION' => 'Alaska Pacific University\, 4101 University Dr\, Anchorage\, AK 99 ', 'SEQUENCE' => '0 ', 'STATUS' => 'CONFIRMED ', 'SUMMARY' => 'Basketball vs Birchwood Christian ', 'TRANSP' => 'OPAQUE ', 'END' => 'VEVENT ', )Alaska Pacific University4101 University DrAnchorageAK99 AK4101+University+Drarray ( 'BEGIN' => 'VEVENT ', 'DTSTART;VALUE=DATE' => '20190624 ', 'DTEND;VALUE=DATE' => '20190629 ', 'RRULE' => 'FREQ=YEARLY;WKST=SU;COUNT=1;INTERVAL=1 ', 'DTSTAMP' => '20191124T081354Z ', 'UID' => '002fe82nhfs3qhfn8i84qe2dm5@google.com ', 'CREATED' => '20181218T234159Z ', 'DESCRIPTION' => ' ', 'LAST-MODIFIED' => '20191122T060518Z ', 'LOCATION' => ' ', 'SEQUENCE' => '1 ', 'STATUS' => 'CONFIRMED ', 'SUMMARY' => 'Soccer Camp (PreK-6th) ', 'TRANSP' => 'OPAQUE ', 'END' => 'VEVENT ', )

: Undefined offset: 1 in ... on line 193

: Undefined offset: 2 in ... on line 194

: Undefined offset: 3 in ... on line 195

: Undefined offset: 1 in ... on line 199

array ( 'BEGIN' => 'VEVENT ', 'DTSTART;VALUE=DATE' => '20190617 ', 'DTEND;VALUE=DATE' => '20190622 ', 'RRULE' => 'FREQ=YEARLY;WKST=SU;COUNT=1;INTERVAL=1 ', 'DTSTAMP' => '20191124T081354Z ', 'UID' => '4e27isqv5puk2ctc2r2pa6vuc9@google.com ', 'CREATED' => '20181218T234147Z ', 'DESCRIPTION' => ' ',
'LAST-MODIFIED' => '20191122T060517Z ', 'LOCATION' => ' ',
'SEQUENCE' => '1 ', 'STATUS' => 'CONFIRMED ', 'SUMMARY' => 'STEM Camp (K-8th) ', 'TRANSP' => 'OPAQUE ', 'END' => 'VEVENT ', )

Why is it doing this?

Angeliss44
  • 125
  • 1
  • 3
  • 20
  • 1
    `print_r($parts)` after the `explode` and you'll probably discover it only has one element. – ceejayoz Nov 24 '19 at 01:28
  • It's not PHP acts as if key is set. It's simply you didn't do enough checks. – Thanh Trung Nov 24 '19 at 01:31
  • Are you trying to escape the comma in your delimiter parameter of `explode()` or is that slash actually in the input string. Your explosion is failing to create the expected number of elements. Your question is offtopic because we don't have enough info to replicate your issue. – mickmackusa Nov 24 '19 at 07:54
  • @ThanhTrung As I was testing I had `echo "SET";` on line 190 and it was returning `SET` on every $icsEvent. This tells me that line 189 must be incorrect. I can't use `array_key_exists` because the array key is always there even if there is not a location entered. Should I be using `isset`? If so, how do I properly perform the check with `isset`? – Angeliss44 Nov 24 '19 at 07:56
  • @mickmackusa The comma is part of the `explode`. The location key has data like: `Alaska Pacific University\, 4101 University Dr\, Anchorage\, AK 99 508\, USA`. The explode works perfectly on the entries that actually have a location data. I have updated my question to include a link to the `ics` file for you to take a look at. – Angeliss44 Nov 24 '19 at 08:00
  • Can't view it on my phone. Post the text. – mickmackusa Nov 24 '19 at 08:02
  • @mickmackusa Just posted the text. – Angeliss44 Nov 24 '19 at 08:07
  • @ceejayoz The `explode` works fine. The problem I'm having lies in the code identifying every location key as having data. – Angeliss44 Nov 24 '19 at 08:09
  • If you are already parsing this document properly, show us `var_export($icsEvent)`. You may need to write a regex pattern to allow for missing parameters in the location. Or, you have something other than an empty string at `if($eventLoc != ""){` – mickmackusa Nov 24 '19 at 08:10
  • @mickmackusa Added `var_export($icsEvent)` to question above. – Angeliss44 Nov 24 '19 at 08:23
  • 1
    I'm not going to answer this one. Too cheap. Use `if(trim($eventLoc) != ""){` – mickmackusa Nov 24 '19 at 08:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/202967/discussion-between-angeliss44-and-mickmackusa). – Angeliss44 Nov 24 '19 at 08:45
  • @Angeliss44 Yes, I know the explode works fine. The end result just doesn't have as many keys as you're checking for sometimes. Check the `isset` of each index you're checking against, or check the `count` of the post-explode array. – ceejayoz Nov 24 '19 at 14:21
  • @ceejayoz I had used `isset` on it originally and it was also returning as true every time even when there was no data in it. mickmackusa helped me solve it by using `if(trim($eventLoc) != ""){`. Now it is only returning data when there is actually something in it. – Angeliss44 Nov 25 '19 at 19:20

1 Answers1

1

Because of the missing parts, you need to do check for existence when accessing an index array.

$locStreet  = isset($parts[1]) ? $parts[1] : '';
Thanh Trung
  • 3,566
  • 3
  • 31
  • 42