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)

2
  • 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? Commented Feb 28, 2012 at 13:08
  • possible duplicate of Is there any good iCal & vCal parser in php(library)? although that is admittedly a somewhat uninformative one Commented Feb 28, 2012 at 13:12

4 Answers 4

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
)
Sign up to request clarification or add additional context in comments.

2 Comments

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.
No you can't do it like this. For example the ";VALUE=DATE" and "INTERVAL" parts are optional.
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] => [email protected]
            [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
        )
)

2 Comments

would this work even if some information is missing? My calendar events do not have location or summary information.
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: code.google.com/p/ics-parser/source/browse/#svn%2Ftrunk
0

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

1 Comment

oh sorry, the actual string doesn't contain variable, I just did that to highlight what I wanted from the string.
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";

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.