Jump to content

pls check


narjis

Recommended Posts

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;

Link to comment
Share on other sites

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])";

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

Ah, I misunderstood your requirement about "one hyphen only" a bit. Or, rather. I took it a bit too literally. :P

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"

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.