wmguk Posted December 4, 2008 Share Posted December 4, 2008 Hey, I ask for a date of birth on my input form, however people put anything from 19.9.74, 19/9/74, 19/09/74, 19.09.74, 19.09.1974, 19/09/1974 and i want the date to always be written as 19/09/1974 Whats the best way to take their info and make it work with my prefered info? Quote Link to comment https://forums.phpfreaks.com/topic/135522-replace-text-on-input/ Share on other sites More sharing options...
Adam Posted December 4, 2008 Share Posted December 4, 2008 Well you could just use three inputs, or three drop down menus to avoid confusion. But if not I suppose you could use a regular expression to extract the 3 parts and then join them back together using your preferred method... Adam Quote Link to comment https://forums.phpfreaks.com/topic/135522-replace-text-on-input/#findComment-705993 Share on other sites More sharing options...
MadTechie Posted December 4, 2008 Share Posted December 4, 2008 Not sure if this helps but you could do this <?php $data = '19.9.74, 19/9/74, 19/09/74, 19.09.74, 19.09.1974, 19/09/1974'; $data = preg_replace('/(\d{1,2}).(\d{1,2}).(19|20)?(\d{2})/i', '\1/\2/\3\4', $data); echo $data; //19/9/74, 19/9/74, 19/09/74, 19/09/74, 19/09/1974, 19/09/1974 ?> EDIT: ooops just re-read.. i thought to had a ton of info and needed to reformat it.. for getting the input you could google JS calander theirs tons and it look fancy or have 2-3 drop downs Quote Link to comment https://forums.phpfreaks.com/topic/135522-replace-text-on-input/#findComment-705998 Share on other sites More sharing options...
Adam Posted December 4, 2008 Share Posted December 4, 2008 Had a quick play, regular expression would look something like: ^[\d]{2}[-\.:\|][\d]{2}[-\.:\|](19|20)[\d]{2}$ .. That would only match a string in format: DD MM YYYY with a delimiter between them of either: - : . | However I do not know how you could use preg_replace() to replace the delimiter with / every time, perhaps somebody else has an idea?? Adam Quote Link to comment https://forums.phpfreaks.com/topic/135522-replace-text-on-input/#findComment-706001 Share on other sites More sharing options...
MadTechie Posted December 4, 2008 Share Posted December 4, 2008 MrAdam's RegEx, with a tweak to capture $data = preg_replace('/^([\d]{2})[-\.:\|]([\d]{2})[-\.:\|]((?:19|20)[\d]{2})$/i', '\1/\2/\3', $data); changing (?:19|20)[\d]{2} to (?:19|20)?[\d]{2} will allow DD MM YY formatting as well Quote Link to comment https://forums.phpfreaks.com/topic/135522-replace-text-on-input/#findComment-706006 Share on other sites More sharing options...
Mark Baker Posted December 4, 2008 Share Posted December 4, 2008 I suppose you could use a regular expression to extract the 3 parts and then join them back together using your preferred method...Although how would a regexp identify whether 12/5/2008 was 12th May 2008 or 5th December 2008? Quote Link to comment https://forums.phpfreaks.com/topic/135522-replace-text-on-input/#findComment-706024 Share on other sites More sharing options...
Adam Posted December 4, 2008 Share Posted December 4, 2008 however people put anything from 19.9.74, 19/9/74, 19/09/74, 19.09.74, 19.09.1974, 19/09/1974 One or two I didn't notice before but, he doesn't appear to be having problems with "15th December 2008".. A maxlength of 10 would also prevent them entering anything like that. Give them a hint? For example: Date of birth: [ ] Eg. 19/9/1974 Would stop the masses entering it wrong, and should account for the odd one who uses a different delimiter! Adam Quote Link to comment https://forums.phpfreaks.com/topic/135522-replace-text-on-input/#findComment-706339 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.