给定一个任意字符串,例如(“下周五我要去玩槌球”
或 “Gadzooks,已经是 6 月 17 日了吗?”
),如何你会从那里提取日期吗?
如果这看起来像是太硬篮子的一个很好的候选者,也许你可以建议一个替代方案。我希望能够解析 Twitter 消息中的日期。我要查看的推文将是用户针对此服务的推文,因此可以指导他们使用更简单的格式,但我希望它尽可能透明。您能想到一个好的中间立场吗?
最佳答案
如果您有能力,可以尝试以下算法。我正在展示一个例子,把繁琐的工作留给你:)
//Attempt to perform strtotime() on each contiguous subset of words...
//1st iteration
strtotime("Gadzooks, is it 17th June already")
strtotime("is it 17th June already")
strtotime("it 17th June already")
strtotime("17th June already")
strtotime("June already")
strtotime("already")
//2nd iteration
strtotime("Gadzooks, is it 17th June")
strtotime("is it 17th June")
strtotime("17th June") //date!
strtotime("June") //date!
//3rd iteration
strtotime("Gadzooks, is it 17th")
strtotime("is it 17th")
strtotime("it 17th")
strtotime("17th") //date!
//4th iteration
strtotime("Gadzooks, is it")
//etc
而且我们可以假设 strtotime("17th June")
比 strtotime("17th")
更准确,因为它包含更多的词...即 "下周五”总是比“周五”更准确。
关于php - 在 PHP 中解析日期字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3053580/