如何使用PHP从ical文件中获取数组中的特定部分

时间:2019-05-15 14:48:58

标签: php

我有文件,我需要以BEGIN:VEVENT开始和以END:VEVENT结尾的所有部分,我需要所有包含数组中所有这些部分的字符串,我在字符串下面

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
CALSCALE:GREGORIAN
BEGIN:VEVENT
LOCATION:Test Location
DESCRIPTION:Test Description
DTSTART:20190507T185657Z
DTEND:20190507T185657Z
SUMMARY:Test Summary
URL:http://xxxxxxxxx
DTSTAMP:20190507T185657Z
UID:5cdaa1cd6c3c3
END:VEVENT
BEGIN:VEVENT
LOCATION:Test Location
DESCRIPTION:Test Description
DTSTART:20190508T185657Z
DTEND:20190508T185657Z
SUMMARY:Test Summary
URL:http://xxxxxxxxx
DTSTAMP:20190508T185657Z
UID:5cdaa1cd6c3cc
END:VEVENT
END:VCALENDAR

我需要数组中的这2个部分

BEGIN:VEVENT
LOCATION:Test Location
DESCRIPTION:Test Description
DTSTART:20190507T185657Z
DTEND:20190507T185657Z
SUMMARY:Test Summary
URL:http://xxxxxxxxx
DTSTAMP:20190507T185657Z
UID:5cdaa1cd6c3c3
END:VEVENT
BEGIN:VEVENT

LOCATION:Test Location
DESCRIPTION:Test Description
DTSTART:20190508T185657Z
DTEND:20190508T185657Z
SUMMARY:Test Summary
URL:http://xxxxxxx
DTSTAMP:20190508T185657Z
UID:5cdaa1cd6c3cc
END:VEVENT

任何人都可以帮助我,我们如何获得呢?

1 个答案:

答案 0 :(得分:1)

使用.*修饰符(?匹配所有字符,包括换行符)在开始和结束定界符之间捕获所有s非贪婪.

$text = file_get_contents('/path/to/file.txt');
preg_match_all('/BEGIN:VEVENT.*?END:VEVENT/s', $text, $result);
print_r($result[0]);