ravi181229 Posted February 26, 2009 Share Posted February 26, 2009 Hi, I would like to format a date. first it should search for . - / (date separators) and if finds split it. I was using : $date_arr = preg_split("/[.|-|\/]/", $date); but it does not work. Please help me with this. Thanks for your time. Ravi Link to comment https://forums.phpfreaks.com/topic/147076-solved-match-separator-and-format-a-date/ Share on other sites More sharing options...
nrg_alpha Posted February 26, 2009 Share Posted February 26, 2009 You pattern is /[.|-|\/]/ Are you trying to use a character class? (if so, this won't work because you are escaping the [ and ] characters...) Is this more along the lines of what you are looking for? #[./-]# I find it easier to use delimiters other than /..../ (especially when there is going to be a / character within the patter.. just saves from having to escape it). I also direct you to have a look at how to ask regex questions. Please take note of #2 and #3 in particular (but reading them all is even better ) Link to comment https://forums.phpfreaks.com/topic/147076-solved-match-separator-and-format-a-date/#findComment-772148 Share on other sites More sharing options...
effigy Posted February 27, 2009 Share Posted February 27, 2009 You can split on \D+. Link to comment https://forums.phpfreaks.com/topic/147076-solved-match-separator-and-format-a-date/#findComment-772590 Share on other sites More sharing options...
nrg_alpha Posted February 27, 2009 Share Posted February 27, 2009 You can split on \D+. That's an even smarter solution. Granted, in that case, couldn't you simply use \D? (unless there is some date format that I'm not thinking of that would require one [or more consecutive] non digit characters, as I'm thinking along the lines of 02/27/09, or 02-27-09, etc...). In either case, your solution is even more simplified. Link to comment https://forums.phpfreaks.com/topic/147076-solved-match-separator-and-format-a-date/#findComment-772681 Share on other sites More sharing options...
effigy Posted February 27, 2009 Share Posted February 27, 2009 I think it's best to include the + since it doesn't hurt. If the data is coming from a user they could have a typo—"02/27//2009" for instance—which, when split, would create 4 elements rather than 3. However, if there are any delimiters on the ends, this would still create empty elements. I recommend: preg_split('/\D+/', $string, -1, PREG_SPLIT_NO_EMPTY) Link to comment https://forums.phpfreaks.com/topic/147076-solved-match-separator-and-format-a-date/#findComment-772703 Share on other sites More sharing options...
nrg_alpha Posted February 27, 2009 Share Posted February 27, 2009 Ah, ok. Wasn't thinking along the lines of typos and what not.. I agree in hind sight.. better to be safe than sorry. Link to comment https://forums.phpfreaks.com/topic/147076-solved-match-separator-and-format-a-date/#findComment-772714 Share on other sites More sharing options...
ravi181229 Posted February 27, 2009 Author Share Posted February 27, 2009 Thanks a lot for you help. Your answers got my work done. Final code (which works according to my expectations) : function excel_MDY2DMY($mdy) { $date_arr = preg_split("/(\.|-|\/)+/", $mdy); $dmy = (count($date_arr) == 1) ? date("Y-m-d", strtotime($date_arr[0])) : $date_arr[2]."-".$date_arr[0]."-".$date_arr[1]; return $dmy; } Thanks for your time. Ravi Link to comment https://forums.phpfreaks.com/topic/147076-solved-match-separator-and-format-a-date/#findComment-772754 Share on other sites More sharing options...
effigy Posted February 27, 2009 Share Posted February 27, 2009 /(\.|-|\/)+/ Why are you using this and not a character class? This kind of alternation is not optimal. Link to comment https://forums.phpfreaks.com/topic/147076-solved-match-separator-and-format-a-date/#findComment-772808 Share on other sites More sharing options...
nrg_alpha Posted February 27, 2009 Share Posted February 27, 2009 Well, I'm glad it works, but I am still confused why you would use "/(\.|-|\/)+/" If you just want to extract the numbers, why not simply use Effigy's pattern instead? $date_arr = preg_split('/\D+/', $mdy, -1, PREG_SPLIT_NO_EMPTY); Note that alternations (...|...|...) are less efficient than character classes [...] or short-hand character classes like \D Link to comment https://forums.phpfreaks.com/topic/147076-solved-match-separator-and-format-a-date/#findComment-772809 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.