reg exp. question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ali90
Forum Newbie
Posts: 9
Joined: Sun Mar 16, 2014 11:03 am

reg exp. question

Post by ali90 »

Hello, I have posted a question here which is from the diploma program I am doing .
In the answer it says that you can use any other method rather than regular expression . can anyone tell me other way of solving this question which could be easy - as i couldnt get to understand regular expression as of now . ( following is a part from Examiner report )

Question - phonenumber field contains only digits, spaces, pairs of parentheses, and an optional "+" sign at the very begining .
if no phone number is provided, make sure that the phone type is "Unknown"


Answer
$phonenumber = $_GET['phonenumber'];
if (!pregmatch("/^\+?[0-9\(\)\s]*$/", $phonenumber))
{
echo "Phone numbers must only contain digits, spaces, parentheses,
and an optional plus sign at the beginning";
}
if ($phonenumber == '')
{
$phonetype = $_GET['phonetype'];
if ($phonetype != 'unknown')
{
echo "Phone type should be set to 'unknown' when phone number is
empty";
}
}
It is not mandatory to use regular expressions, any reasonable equivalent will
do.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: reg exp. question

Post by Celauran »

In the answer it says that you can use any other method rather than regular expression
It is not mandatory to use regular expressions
Which is it? Can you use regex or not?
ali90
Forum Newbie
Posts: 9
Joined: Sun Mar 16, 2014 11:03 am

Re: reg exp. question

Post by ali90 »

One can solve the answer using regular expression but the examiner report says that we can also use other methods to solve this and I wanted to know what can be other method to solve this question other than regular expression
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: reg exp. question

Post by Celauran »

Regex is really the right tool for the job here. You could also create an array of allowed characters and compare each character in the phone number against that list, but that's just cumbersome.
ali90
Forum Newbie
Posts: 9
Joined: Sun Mar 16, 2014 11:03 am

Re: reg exp. question

Post by ali90 »

Thanks a lot
Post Reply