dpacmittal Posted May 11, 2010 Share Posted May 11, 2010 First of all, sorry for not being able to provide a more appropriate title. Write a function declared as function ReformatPhoneNumber($number), whose argument will contain string data representing some phone number data (entered by the user). A valid phone number may consist of between 7 and 12 digits (0..9). Assume that in between some adjacent digits there may optionally appear either a single space, or a single hyphen (-). Any other phone number should be considered invalid. If the phone number is valid, the return value of your function should contain a string containing between 7 and 12 digits, representing the same phone number after removing all hyphens and spaces. If the phone number is invalid, throw a standard PHP5 Exception initialized with the text "Invalid phone number". The first and the last character of the string should be a number. For example, after calling ReformatPhoneNumber('012-345 69') the return value should be '01234569'. Calling the function with any of these values: '012345', '-012345 678', '01203- 34566', '123456678875432', '1234x567' should result in an exception. This is a question from elance coding test. I completed it last time by using all sorts of functions and a very basic regex. This time I tried to become brave and thought of solving it completely using Regexes. However I got stuck at multiple points (hence, the inappropriate title). I tried these which are obviously wrong: if(preg_match('!^\d([\d]+(\s|\-)?){5,10}\d$!',$number)) I know it would accept any length of string. if(preg_match('!^\d(\d|\s|\-){5,10}\d$!',$number)) This would allow multiple spaces and hyphens next to each other. Normally, my knowledge on regex usually gets me through my work and I don't usually need help in this regard. However, this one completely got me. Any help would be appreciated. Thanks! Link to comment https://forums.phpfreaks.com/topic/201358-regex-help/ Share on other sites More sharing options...
dpacmittal Posted May 11, 2010 Author Share Posted May 11, 2010 UPDATE: I tried this new regex. It seems to be working fine except that it is accepting phone numbers with lesser digits. Here is it: if(preg_match('!^\d(\d|(?<=\d)(\s|\-)?){5,10}\d$!',$number)) Link to comment https://forums.phpfreaks.com/topic/201358-regex-help/#findComment-1056454 Share on other sites More sharing options...
Adam Posted May 11, 2010 Share Posted May 11, 2010 function ReformatPhoneNumber($number) { if (preg_match('/^(\d[ -]?){7,12}$/', $number, $matches)) { return preg_replace('/[ -]/', '', $number); } throw new Exception('Invalid phone number'); } Link to comment https://forums.phpfreaks.com/topic/201358-regex-help/#findComment-1056490 Share on other sites More sharing options...
Adam Posted May 11, 2010 Share Posted May 11, 2010 Actually you may want to use: /^(\d[ -]?){7,12}[^ -]$/ This will prevent the last digit being a space or hyphen. Link to comment https://forums.phpfreaks.com/topic/201358-regex-help/#findComment-1056493 Share on other sites More sharing options...
dpacmittal Posted May 11, 2010 Author Share Posted May 11, 2010 Actually you may want to use: /^(\d[ -]?){7,12}[^ -]$/ This will prevent the last digit being a space or hyphen. Thanks a lot. I was just over-complicating stuff. It was straighforward. Thanks, again. Link to comment https://forums.phpfreaks.com/topic/201358-regex-help/#findComment-1056495 Share on other sites More sharing options...
Adam Posted May 11, 2010 Share Posted May 11, 2010 No probs Link to comment https://forums.phpfreaks.com/topic/201358-regex-help/#findComment-1056496 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.