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! Quote Link to comment 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)) Quote Link to comment 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'); } Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Adam Posted May 11, 2010 Share Posted May 11, 2010 No probs Quote Link to comment 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.