Question
here all the questions I need only PHP Exercise 3-3 PHP Exercise 3-1 In this exercise, you will create a script that validates whether a
here all the questions I need only PHP Exercise 3-3
PHP Exercise 3-1
In this exercise, you will create a script that validates whether a credit card number contains only integers. The script will remove dashes and spaces from the string. After the dashes and spaces are removed, the script should reject the credit card number if it contains any other non-numeric characters.
- Create a folder named PHPEx3 on your h drive or on your home computer if you are completing this at home. Create a new html document in your text editor.
- Be sure to include all the appropriate html tags for a basic document. Add id comments at the top containing: your name, the date, and the words PHP Exercise 3-1
- Add the following text and elements to the document body:
Validate Credit Card
- Add the following script section to the document body:
?>
- Declare a $CreditCard array that contains three values: an empty string, a valid credit card number with numbers and dashes, and a credit card number with four initial uppercase
letter Os.
$CreditCard = array(
"",
"8910-1234-5678-6543",
"OOOO-9123-4567-0123");
- Add the following statements to iterate through each of the elements in the
$CreditCardarray to determine if the element contains a value.
foreach ($CreditCard as $CardNumber) {
if (empty($CardNumber))
echo "
This Credit Card Number is
invalid because it contains an empty
string.
";- Add the following else clause to validate the credit card number. The code uses str_replace()
functions to remove any dashes and spaces in the number. Then, a nested if...else
statement checks whether the new value is numeric. If the number is not numeric, a warning is displayed. If the number is numeric, the modified credit card number is displayed in the Web browser.
else {
$CreditCardNumber = $CardNumber;
$CreditCardNumber = str_replace("-", "",
$CreditCardNumber);
$CreditCardNumber = str_replace(" ", "",
$CreditCardNumber);
if (!is_numeric($CreditCardNumber))
echo "
Credit Card Number " .
$CreditCardNumber . " is not a valid
Credit Card number because it contains
a non-numeric character.
";else
echo "
Credit Card Number " .
$CreditCardNumber . " is a valid
Credit Card number.
";}
}
- Save the document as ValidateCreditCard.php in the PHPEx3 folder.
PHP Exercise 3-2 In this exercise, you will create a script that uses comparison operators and functions to compare two strings to see if they are the same. You will use this same technique in Lab 2.
- Create a new document in your text editor.
- Be sure to include all the appropriate html tags for a basic document. Add id comments at the top containing: your name, the date, and the words PHP Exercise 3-1
- Add the following text and elements to the document body:
Compare Strings
- Add the following script section to the document body:
?>
- In the script section, declare and initialize two string variables:
$firstString = "Geek2Geek";
$secondString = "Geezer2Geek";
- Add the following if statement to the script section. If both the $firstString and $secondString
contain a value, the statements in the ifstatement execute. The nested if statement uses the comparison operator (==) to determine if both strings are the same. If the strings are not the same, the elseclause uses the similar_text()and levenshtein()functions to compare the strings.
if ( !empty($firstString) && !empty($secondString)) {
if ($firstString == $secondString)
echo "
Both strings are the same.
";else {
echo "
Both strings have "
. similar_text($firstString,
$secondString)
. " character(s) in common.
";
echo "
You must change " .
levenshtein($firstString,
$secondString) . " character(s) to
make the strings the same.
";
}
}
- At the end of the script section, add the following else clause, which executes if either the
$firstString or the $secondStringcontains an empty value.
else
echo "
Either the \$firstString variable or
the \$secondString variable does not
contain a value so the two strings cannot
be compared.
";- Save the document as CompareStrings.php in the PHPEx3 folder
PHP Exercise 3-3 In this exercise, you will create a script that uses regular expressions to validate that an e-mail address is valid for delivery to a user at example.org. For an e-mail address to be in the correct format, only username or first.last may appear before the @ symbol, and only example.org or mail.example.org may appear after the @ symbol.
- Create a new document in your text editor.
- Be sure to include all the appropriate html tags for a basic document. Add id comments at the top containing: your name, the date, and the words PHP Exercise 3-3. Use Validate Local Address as the content of the
element. - Add the following text and elements to the document body:
Validate Local Address
- Add the following script section to the document body:
?>
- In the script section, declare an $email array that contains
five e-mail addresses:
$email = array(
"jsmith123@example.org",
"john.smith.mail@example.org",
"john.smith@example.org",
"john.smith@example",
"jsmith123@mail.example.org");
- Add the following statements to iterate through each of the elements in the $emailarray to determine if it is in the correct format:
foreach ($email as $emailAddress){
echo "The email address “" . $emailAddress .
"” ";
if (preg_match("/^(([A-Za-z]+\d+)|" .
"([A-Za-z]+\.[A-Za-z]+))" .
"@((mail\.)?)example\.org$/i",
$emailAddress)==1)
echo " is a valid e-mail address.";
else
echo " is not a valid e-mail address.";
}
- Save the document as ValidateLocalAddress.php in the Projects directory for Chapter 3 and upload the file to the server.in the PHPEx3 folder.
- Zip all three files in the PHPEx3 folder. Upload the zipped file to the PHP Ex3 file upload folder.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started