Jump to content

How to find date within a string?


python72

Recommended Posts

I have another question related to string searches.

What I have is a string of couple thousand characters of all types but the date format is allways the same: dd-mth-year (two digits for day-three letters for month-four digits for year). Can I use the format alone to determine where the date is located which in turn would make it very easy to store that date into variables for inserting into database, etc.

Is there anyway to tell php the format to look for or do I have to have actual numbers/letters to perform the search?

 

Link to comment
Share on other sites

You can use regular expressions. Just keep in mind that what you get out won't be any better than what you put in.

var_dump(preg_match_all('/\d\d-[a-z][a-z][a-z]-\d\d\d\d/i', $text));

What I mean by that is that it would be a Good Idea to be more specific than "three letters" for the month. List the possibilities out.

(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)

Link to comment
Share on other sites

One more pattern question which just came up and the way I am trying to do it is not working.

If the date is in the following format: December 04, 2010 I have to came up with different pattern, I thought that the following pattern would work:

$Pattern='/(January|February|March|April|May|June|July|August|September|October|November|December) \d\d, \d\d\d\d/i';

but it does not work, not sure how to fix it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.