1

I have a string:

$string = "Created on: 06-Sep-08";

I would like to match the date and convert it to 06/09/2008; what is the needed regexp?
Thanks

2 Answers 2

4
$matches = array();
preg_match('/(\d{2})-(\D*)-(\d{2})/', $string, $matches)

This returns the array matches.

$matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on

http://php.net/manual/en/function.preg-match.php

Sign up to request clarification or add additional context in comments.

2 Comments

this works! thanks a lot. what is the difference between \d and \D?
\d matches digit's and \D matches non-digit -characters. A full list can be found at <en.wikipedia.org/wiki/Regex#POSIX_character_classes>
3
preg_match('/\:.*/is',...);

And then do a

strtotime(...)

EDIT:

Forgot to add the parantheses

preg_match('/\:(.*)/is',$string,$matches);

1 Comment

preg_match('/\:(.*)/is',$string,$matches);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.