narjis Posted July 29, 2012 Share Posted July 29, 2012 I have to pass a string inthe function which matches only numbers and if one '- ' comes it is a valid string but more than one hyphen together gives exception. Output should be a series of number. E.g. Calling the function with any of these values: '012345', '-012345 678', '01203- 34566', '1234x567' should result in an exception Here is my code but it is not working for the cases function ReformatPhoneNumber($number){ try{ if(!preg_match('/[0-9]*(-)*$/',$number)) throw new Exception("Only numbers are allowed\n"); //return false; //else{ return preg_replace('/(-)*/','',$number); //echo $number;//} } catch(Exception $e){ echo ($e->getMessage()); } } $num = ReformatPhoneNumber('0-12-345n69'); echo $num; Quote Link to comment Share on other sites More sharing options...
Christian F. Posted July 29, 2012 Share Posted July 29, 2012 The following RegExp should do wonders, if I've understood your request correctly. $RegExp = '/^\\d+(?:-\\d+)?\\z/'; Since you've validating a phone number, you have to remember to bind the RegExp to the start (^) and end (\z) of the string. Otherwise I'll match as long as there's just a single digit in there somewhere. Regardless of how the rest looks like. To make sure you only get digits, after validating, just run "$number = str_replace ('-', '', $number[0])"; Quote Link to comment Share on other sites More sharing options...
narjis Posted July 29, 2012 Author Share Posted July 29, 2012 The expression is working fine but when I'm giving $number='0-12-345-69' it is giving Exception Here is the whole code. function ReformatPhoneNumber($number){ $len = strlen($number); $RegExp = '/^\\d+(?:-\\d+)?\\z/'; $newNum=''; try{ if (($len <7)||($len>12)){ throw new Exception("Number of invlaid length\n"); } if(!preg_match($RegExp,$number)) throw new Exception("Only numbers are allowed\n"); echo preg_replace('/-/','',$number); } catch(Exception $e){ echo ($e->getMessage()); } } $num = ReformatPhoneNumber('0-12-3-69'); echo $num; Quote Link to comment Share on other sites More sharing options...
Christian F. Posted July 29, 2012 Share Posted July 29, 2012 Ah, I misunderstood your requirement about "one hyphen only" a bit. Or, rather. I took it a bit too literally. In any case, the fix is simple. Just change the last question mark in the RegExp to a star. That'll change it from matching "0 or 1" to "0 or many" Quote Link to comment Share on other sites More sharing options...
narjis Posted July 30, 2012 Author Share Posted July 30, 2012 Thanx alot it is working fine can u explain the Regex since i'm abit weak. Also how can I impore in regular expressions. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted July 30, 2012 Share Posted July 30, 2012 I recommend Regular-Expressions.info to learn more about RegExps. Using its library of good information, you should be able to piece together what the RegExp actually does. Should be a very good exercise to learn RegExps. Glad I could be of help. PS: Note that I've double escaped the values since we're working inside a PHP string here, and backslash is a meta-character in PHP strings as well. 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.