开发者

Extracting date from String

I'd like to extract a开发者_C百科 date from any string entered. But not specific to a date format (otherwise I'd use some sort of regex)

e.g

"Pictures taken in Africa 3 weeks ago"

The php parser would extrace "3 weeks ago" and I can happily convert this.

Does anyone know of the best method to perform this or of any library that can do it for me. I determined looping through all possible combinations of strings within the input wouldn't b a good idea.

Thanks guys


Pekka made a good point.

In the event of multiple strings detected for use with the strtotime() function, I would perform seom magic on my part. Maybe designing some sort of conetext algorithm. But I don't expect you chaps to have to worry about that bit as well. I'm uber happy to revieve the understanding of how to extract this date bit.

More Examples

last week at the albert hall concert.

taken 5 years ago in paris with Emily.

All these pictures we taken at the new place in december

  • Stuff like that really.


I take it you're using strtotime. In that case, why not parse the string, and determine the longest list of tokens that could make a valid time, such as 'ago', '3', 'weeks', 'days' etc. Then try to parse the list of tokes with strtotime. If this doesn't work, try the next longest list of valid tokens. You'll obviously need a list of all the valid tokens strtotime will take, but this shouldn't be too hard.

So, in "Pictures taken in Africa 3 weeks ago", your code would find the valid tokens "3", "weeks" and "ago", and try to parse them with strtotime.

It's not perfect, but I think it would work in a lot of cases.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜