Page 1 of 1

reg exp. question

Posted: Tue Mar 18, 2014 12:00 pm
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.

Re: reg exp. question

Posted: Tue Mar 18, 2014 12:03 pm
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?

Re: reg exp. question

Posted: Tue Mar 18, 2014 2:01 pm
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

Re: reg exp. question

Posted: Tue Mar 18, 2014 2:17 pm
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.

Re: reg exp. question

Posted: Wed Mar 19, 2014 10:09 am
by ali90
Thanks a lot