简体   繁体   English

Pregreplace 在 php 中的一个 ics 文件上

[英]Pregreplace on an ics file in php

Very short version of this question is "How do I remove all lines of a multiline string if they don't contain one of many keywords in the line?"这个问题的非常简短的版本是“如果行中不包含许多关键字之一,我如何删除多行字符串的所有行?”

i'm making an app in php and js and really don't want to use many outside libraries to bloat my code and file structure also I'm not a huge fan of classes and methods (probably a bit of OCD on my part).我正在 php 和 js 中制作一个应用程序并且真的不想使用许多外部库来膨胀我的代码和文件结构我也不是类和方法的忠实粉丝(我可能有点强迫症) .

This code doesn't use the google API rather it uses the ics file because I wanted to make it more versatile此代码不使用 google API 而是使用 ics 文件,因为我想让它更通用

Long story short, I'm parsing an ics file to compare possible dates/times a client might book a meeting with a google calendar ics file.长话短说,我正在解析一个 ics 文件,以比较客户可能用谷歌日历 ics 文件预订会议的可能日期/时间。 The goal is that I only offer meeting times that don't conflict with my schedule and that's working well so far.我的目标是只提供与我的日程安排不冲突并且到目前为止运作良好的会议时间。 I have a good javascript parser I made to get the start and end times.我有一个很好的 javascript 解析器来获取开始和结束时间。

The issue is I'm sending my entire calendar to the client in the source code that's 10 years of events which makes the source code like 15k lines.问题是我将我的整个日历发送给源代码中的客户端,这是 10 年的事件,这使得源代码像 15k 行。 I'm also not crazy about strangers having access to that much detail with my schedule and private info.我也不喜欢陌生人了解我的日程安排和私人信息的那么多细节。 I'd like a php script to remove the lines that don't start with DTSTART or DTEND but when I see a pregreplace I glaze over I have a hard time understanding it.我想要一个 php 脚本来删除不以 DTSTART 或 DTEND 开头的行,但是当我看到一个 pregreplace 时,我呆住了,我很难理解它。 I use them but I have a hard time with them.我使用它们,但我很难使用它们。

What's the pregreplace to keep the entire line of an ics if it starts with "DTSTART" or "DTEND" and remove everything else?如果 ics 以“DTSTART”或“DTEND”开头并删除其他所有内容,那么保留整行 ics 的前提是什么?

There are probably a few other lines I'll have to add to keep my javascript ics parser from breaking too.可能还有几行我必须添加以防止我的 javascript ics 解析器也被破坏。 But I really want to solve the privacy issue.但我真的很想解决隐私问题。 In a perfect world I'd also get rid of events that have already ended while I'm parsing because they can't conflict with future events.在一个完美的世界中,我还会在解析时摆脱已经结束的事件,因为它们不会与未来的事件发生冲突。

Here is a chunk of text from the ics file I've redacted some trivial private info just in case这是 ics 文件中的一段文本我已经编辑了一些琐碎的私人信息以防万一

BEGIN:VEVENT
DTSTART:20200907T170000Z
DTEND:20200907T180000Z
DTSTAMP:20221231T224008Z
UID:(redacted)
CREATED:20200820T161956Z
DESCRIPTION:
LAST-MODIFIED:20200820T162009Z
LOCATION:
SEQUENCE:1
STATUS:CONFIRMED
SUMMARY:Contact (redacted name here)
TRANSP:OPAQUE
END:VEVENT
BEGIN:VEVENT
DTSTART;VALUE=DATE:20201101
DTEND;VALUE=DATE:20201104
DTSTAMP:20221231T224008Z
UID:(redacted)
CREATED:20200818T153428Z
DESCRIPTION:
LAST-MODIFIED:20200818T153428Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:..REDACTED PRIVATE INFO
TRANSP:TRANSPARENT
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:This is an event reminder
TRIGGER:-P0DT4H0M0S
END:VALARM
END:VEVENT
BEGIN:VEVENT

From this chunk I'd like to only keep lines like these (or ideally ones with DTEND:2023 and up):从这个块中,我只想保留这样的行(或者理想情况下使用 DTEND:2023 及以上的行):

BEGIN:VEVENT
DTSTART:20200907T170000Z
DTEND:20200907T180000Z
END:VEVENT
BEGIN:VEVENT
DTSTART;VALUE=DATE:20201101
DTEND;VALUE=DATE:20201104
END:VEVENT
BEGIN:VEVENT...

Here is the solution I found that works so far first you explode each line of the text on EOL.这是我发现到目前为止有效的解决方案,首先你在 EOL 上分解文本的每一行。 then this preg_match plus an array filter finds any strings in the list I started with just the start and end times.然后这个 preg_match 加上一个数组过滤器会在我以开始和结束时间开始的列表中找到任何字符串。 I think this will work for what I'm doing.我认为这对我正在做的事情有用。

$ics = file_get_contents("https://calendar.google.com/calendar/ical/s.../basic.ics");

$lines = explode(PHP_EOL, $ics);
$remaining = array_filter($lines, function($line) {
    return (preg_match('(DTSTART|DTEND)', $line) == 1);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM