1

I need help parsing a string.

The string is:

DTSTART;VALUE=DATE:**20120201** DTEND;VALUE=DATE:20120202 RRULE:FREQ=**DAILY**;INTERVAL=**2**;UNTIL=**20120331**

(the variables I need have ** on either side of them)

I need to assign the values in bold to the following variables (in the same order):

$startdate 
$frequency
$interval
$enddate

All help is appreciated. Thanks!

EDIT: the actual string is:

DTSTART;VALUE=DATE:20120201 DTEND;VALUE=DATE:20120202 RRULE:FREQ=DAILY;INTERVAL=2;UNTIL=20120331

I only added ** on either side of the variables to show which ones I wanted (as bold highlighting does not work in the code snippet)

  • Given the term "the right tool for the right job", have you looked into an iCalendar parser, since the data is clearly a line from a block of iCal data? – CodeCaster Feb 28 '12 at 13:08
  • possible duplicate of [Is there any good iCal & vCal parser in php(library)?](http://stackoverflow.com/questions/7508800/is-there-any-good-ical-vcal-parser-in-phplibrary) although that is admittedly a somewhat uninformative one – Gordon Feb 28 '12 at 13:12

4 Answers4

1

You can do it like this :

<?php
$s = "DTSTART;VALUE=DATE:**20120201** DTEND;VALUE=DATE:20120202 RRULE:FREQ=**DAILY**;INTERVAL=**2**;UNTIL=**20120331**";

preg_match(
    '/^DTSTART;VALUE=DATE:\*\*(\d+)\*\*\s+DTEND;VALUE=DATE:(\d+)\s+RRULE:FREQ=\*\*(\w+)\*\*;INTERVAL=\*\*(\d+)\*\*;UNTIL=\*\*(\d+)\*\*/',
    $s,
    $matches
);

print_r($matches);
?>

-----8<--------------------------------------------------------------------------------

php file.php

Array
(
    [0] => DTSTART;VALUE=DATE:**20120201** DTEND;VALUE=DATE:20120202 RRULE:FREQ=**DAILY**;INTERVAL=**2**;UNTIL=**20120331**
    [1] => 20120201
    [2] => 20120202
    [3] => DAILY
    [4] => 2
    [5] => 20120331
)
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • HI, thank you for your reply. I should I have said that the ** on either side of the variables was added by me to highlight which variables I wanted. The string isn't actually like that. – user1233852 Feb 28 '12 at 13:16
  • 1
    No you [can't](http://tools.ietf.org/html/rfc4791) do it like this. For example the ";VALUE=DATE" and "INTERVAL" parts are optional. – CodeCaster Feb 28 '12 at 13:18
1

Have a look at this PHP class: http://code.google.com/p/ics-parser/

It converts the iCal string into an array so its super easy to handle.

Array
(
    [0] => Array
        (
            [DTSTART] => 20110105T090000Z
            [DTEND] => 20110107T173000Z
            [DTSTAMP] => 20110121T195741Z
            [UID] => 15lc1nvupht8dtfiptenljoiv4@google.com
            [CREATED] => 20110121T195616Z
            [DESCRIPTION] => This is a short description\nwith a new line. Some "special" 'signs' may be <interesting>\, too.
            [LAST-MODIFIED] => 20110121T195729Z
            [LOCATION] => Kansas
            [SEQUENCE] => 2
            [STATUS] => CONFIRMED
            [SUMMARY] => My Holidays
            [TRANSP] => TRANSPARENT
        )
)
472084
  • 17,666
  • 10
  • 63
  • 81
  • would this work even if some information is missing? My calendar events do not have location or summary information. – user1233852 Feb 28 '12 at 13:20
  • Yes, it would still work, but location or summary would not be in the array. Using a pre-made class is nearly always the best route to go down for things like this. I have used this one before and couldn't fault it. I should mention you can download it here: http://code.google.com/p/ics-parser/source/browse/#svn%2Ftrunk – 472084 Feb 28 '12 at 13:22
0

if your required value is within ** you can use explode(string,'**') and can use alternate array value

Mukesh
  • 63
  • 1
  • 8
  • oh sorry, the actual string doesn't contain **variable**, I just did that to highlight what I wanted from the string. – user1233852 Feb 28 '12 at 13:11
0

At the following example i explode you string and do some regex and string replaces.. There is may a better approach, since iam not a regex pro, but this should work!

$teststr="DTSTART;VALUE=DATE:**20120201** DTEND;VALUE=DATE:20120202 RRULE:FREQ=**DAILY**;INTERVAL=**2**;UNTIL=**20120331**";
$array=explode(";",$teststr);

preg_match("/\*(.*)\*/", $array[1], $matches);
$startdate = str_replace("*", "", $matches[1]);
echo "Startdate:". $startdate ."<br>\n";

preg_match("/\*(.*)\*/", $array[2], $matches);
$frequency = str_replace("*", "", $matches[1]);
echo "Frequency:". $frequency ."<br>\n";

preg_match("/\*(.*)\*/", $array[3], $matches);
$interval = str_replace("*", "", $matches[1]);
echo "Interval:". $interval  ."<br>\n";

preg_match("/\*(.*)\*/", $array[4], $matches);
$enddate = str_replace("*", "", $matches[1]);
echo "Enddate:" . $enddate ."<br>\n";
nfo
  • 637
  • 2
  • 6
  • 19
  • Thank you for your reply. I have updated my question as I didn't explain it properly. I'd appreciate it if you could read it and provide another answer. – user1233852 Feb 28 '12 at 13:22