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