-1

for example there is a data

$string = "my birthday is on July 11 2014"

how do you get the date within the string?

user3325919
  • 45
  • 1
  • 7
  • Is the date always in the same format? – Jorg Mar 24 '14 at 01:33
  • Might be better to show some code so that folks know you're doing the work too. Write a function looking for the names of months using regular expressions. Here's the [second result of my google search](http://forums.phpfreaks.com/topic/220427-how-to-find-date-within-a-string/) – jonlink Mar 24 '14 at 01:39
  • Yes it is always in the same formet :) – user3325919 Mar 24 '14 at 01:40

1 Answers1

2
$string = 'Mr Roux said it will be argued that on July 11 2014 it was physically impossible for Ms Steenkamp to have continued to scream after being struck in the head by a bullet from gun, especially if the sound had to travel 177 metres from the bathroom where the young model cowered.\"A person with that brain damage will have no cognitive response,\" he said.A short time later Dr Burger’s evidence was complete, herself fighting back tears at having to relive the night in question.\"It was awful to hear her shout before the shots,\" she said, saying she was still plagued by the screams. \"Its still quite raw.\" The Oscar Pistorius';

preg_match_all('/\b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May?|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sept(?:ember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?) (\d|\d{2}) \d{4}/', $string, $matches);

//flattens the array, because it is bi-dimensional
$flattened_array = new RecursiveIteratorIterator(new RecursiveArrayIterator($matches));

//loop through this flattened array
foreach($flattened_array as $k => $v)
{
        //if the value of that array item is not a pure integer (11 or 12, etc..)
    if((int) $v === 0)
    {
            //add the value to the final output array
        $final_matches[] = $v;
    }       
}

var_dump($final_matches);
//returns array(2) { [0]=> string(12) "July 11 2014" [1]=> string(12) "July 12 2014" }

The regex will match anything with Jan(uary), Feb(ruary), Mar(ch) etc... followed by a space, then by X or XX (digits), then a space, then XXXX.

Lee Salminen
  • 900
  • 8
  • 18
  • days 1-9 ? unlikely to see in print `July 01 2014 ` more likely `July 1 2014 ` –  Mar 24 '14 at 01:49
  • ok, but in 8 thousand years this will fail :-) –  Mar 24 '14 at 01:58
  • :P ... if this happens to be the only line of code in the OP's application that needs to change after 8000 years, then he will have turned out to be the world's best developer, and I would like to buy him/her a beer. – Lee Salminen Mar 24 '14 at 01:59
  • it also gets another 11? – user3325919 Mar 24 '14 at 02:20
  • @user3325919 Yes, but these 2 return values are separated by an array. So, you can do: `echo $matches[0]` . – Lee Salminen Mar 24 '14 at 02:21
  • my problem is when there are two date for example July 11 2014 and July 12 2014 it will only get july 11 2014. why is that? – user3325919 Mar 24 '14 at 02:29
  • @user3325919 use preg_match_all() with the same parameters. I'll update my response above. Don't forget to mark my answer as accepted :). – Lee Salminen Mar 24 '14 at 02:31
  • what is the meaning of this in your pattern? (\d|\d{2}) \d{4}/' – user3325919 Mar 24 '14 at 03:01
  • `\d` means digit 0-9. `\d{2}` means 2 digits 0-9. The `|` means or. \d{4} means 4 digits 0-9. So, literally it means (either a 1 digit or 2 digit number) space 4 digit number. – Lee Salminen Mar 24 '14 at 03:15
  • how about this? ->/\b(?:Jan(?:uary)? sorry I have alot of questions. I am new in using regex, patterns. etc. – user3325919 Mar 24 '14 at 03:28
  • That means a string that is either Jan or January. It will return true for either. See here for more detail: http://stackoverflow.com/questions/2655476/regex-to-match-month-name-followed-by-year – Lee Salminen Mar 24 '14 at 03:31
  • now I get it. thank you so much. I have another question, how do I get rid of the last two digit that appears when I do foreach to extract the date? In getting the July 11 2014 and July 12 2014 it still gets the number 11 and 12 seperately? – user3325919 Mar 24 '14 at 03:34
  • @user3325919 see my updated answer above. – Lee Salminen Mar 24 '14 at 03:40
  • that explained it better. thank you again sir! One more thing sir, why do you have to flatten a two dimensional array? what is the difference between flattening it and just using the variable $matches? – user3325919 Mar 24 '14 at 03:54
  • No worries :). If the array wasnt flattened, then you would have to do a loop inside of a loop, and i think those are ugly. The position of the values is not important in this case, so flattening them isnt a problem. – Lee Salminen Mar 24 '14 at 03:56
  • okidoki. really helped me alot ;) – user3325919 Mar 24 '14 at 04:01
  • (int) $v === 0 that statement still confuses me though? – user3325919 Mar 24 '14 at 05:20
  • what if what I want to extract is 2014 January 1? I tried this but it won't work preg_match_all('/(\d{4}/) \b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May?|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sept(?:ember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?) ', $paragraph, $date); – user3325919 Mar 24 '14 at 07:37