PHP 解析 iCal 提醒字符串

发布于 2024-10-06 04:09:17 字数 356 浏览 6 评论 0原文

我目前正在解析 iCal 提醒字符串,类似于: -P14DT0H0M0S

使用 PHP,我如何能够解析字符串的元素,以便如果我有:

<?
 $reminder = "-P14DT0H0M0S"  // somehow output to show "-2 weeks" or eve "-14 days"

             //  OR  //

 $reminder = "-P0DT3H0M0S"  // somehow output to "-3 hours" 

 // etc...

对此的任何帮助都是伟大的。我有点不知道从哪里开始。

非常感谢!

I'm currently working on parsing iCal reminder strings, similar to: -P14DT0H0M0S

Using PHP, how might I be able to parse the elements of the string so that if I had:

<?
 $reminder = "-P14DT0H0M0S"  // somehow output to show "-2 weeks" or eve "-14 days"

             //  OR  //

 $reminder = "-P0DT3H0M0S"  // somehow output to "-3 hours" 

 // etc...

Any help on this would be great. I'm kind of stuck on where to begin.

Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮华 2024-10-13 04:09:17

我不熟悉这种格式,几乎可以肯定有一个可用的库,但看起来一个简单的正则表达式就可以完成这里的工作。

像这样的东西:

$matches = array();
if (preg_match('/^-P(\d+)DT(\d+)H(\d+)M(\d+)S$/', $reminder, $matches))
{
  // matched pattern, elements captured with () will be stored in $matches[1..]
  $days = $matches[1];
  $hours = $matches[2]; 
  $minutes = $matches[3];
  $seconds = $matches[4];
}

I'm not familiar with the format, and there's almost certainly a library available for this, but it looks like a simple regex would do the job here.

Something like:

$matches = array();
if (preg_match('/^-P(\d+)DT(\d+)H(\d+)M(\d+)S$/', $reminder, $matches))
{
  // matched pattern, elements captured with () will be stored in $matches[1..]
  $days = $matches[1];
  $hours = $matches[2]; 
  $minutes = $matches[3];
  $seconds = $matches[4];
}
酒儿 2024-10-13 04:09:17

不确定您的项目背景或最终结果是什么,但这可能会有所帮助:

Not sure what the background of your project or your end result is, but this may help:

衣神在巴黎 2024-10-13 04:09:17

谷歌日历可以完全删除时间部分并返回类似“-P7D”的内容,所以我进行了调整
therefromhere 的正则表达式来处理这个问题。

preg_match('/^-P(\d+)DT?(\d+)?H?(\d+)?M?(\d+)?S?$/', $reminder, $matches);

Google calendar can drop time part completely and return something like '-P7D' so I have tweaked
therefromhere's regexp to deal with that.

preg_match('/^-P(\d+)DT?(\d+)?H?(\d+)?M?(\d+)?S?$/', $reminder, $matches);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文