我需要帮助解析字符串。
字符串是:
DTSTART;VALUE=DATE:**20120201** DTEND;VALUE=DATE:20120202 RRULE:FREQ=**DAILY**;INTERVAL=**2**;UNTIL=**20120331**
(我需要的变量**在它们的两边)
我需要将粗体值分配给以下变量(按相同顺序):
$startdate
$frequency
$interval
$enddate
感谢所有帮助。谢谢!
编辑:实际字符串是:
DTSTART; VALUE = DATE: 20120201 DTEND; VALUE = DATE:20120202 RRULE:FREQ = DAILY ; INTERVAL = 2 ; UNTIL =的 20120331
我只在变量的两边添加**以显示我想要的那些(因为粗体突出显示在代码片段中不起作用)
答案 0 :(得分:1)
你可以这样做:
<?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&LT; ---------------------------------------- ----------------------------------------
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
)
答案 1 :(得分:1)
看一下这个PHP课程:http://code.google.com/p/ics-parser/
它将iCal字符串转换为数组,因此非常容易处理。
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
)
)
答案 2 :(得分:0)
如果您所需的值在**范围内,则可以使用explode(string,'**')并且可以使用替代数组值
答案 3 :(得分:0)
在下面的示例中,我将您的字符串分解并执行一些正则表达式和字符串替换。 可能有更好的方法,因为我不是正则表达式专家,但这应该有效!
$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";