Lec 13 PHP
Lec 13 PHP
Once the data is received we can use PHP to validate the user’s input. In PHP, usually we
declare a regular expression and the use a PHP function (preg_match()) to compare user’s input
with the regular expression.
*: matches the character, class or sub-pattern 0 or any number of time equals {0, }
RE examples: in the following examples, we write some regular expressions to validate user’s
input
Validating date: dates are the most common input we need to take from a user. A date consists
of three parts: day, month and year. These parts are usually separated by – or / symbol. To
validate a date we can use the following regular expression. This expression consists of three
parts. In each part the allowed character’s range is given \d which means exactly the same as
[0-9] and are separated by – symbol.
– |^\d{2}-\d{2}-\d{4}$|
Validating CNIC: In the following example, we have declared a regular expression to validate
CNIC number. A CNIC number consists of three parts. The first part consists of five digits, then a
– symbol. The second part consists of seven digits and then again a – symbol; and then one
digit.
– |^\d{5}-\d{7}-\d{1}$|
Validating Email: The following example validates an email address.
– |^[a-zA-Z0-9_.]+@[a-z]{3,5}.[a-z]{2,3}$|
Evaluating user’s input: After declaring a regular expression, we have to match the user’s
input.The preg_match() function searches a string for a specific pattern, returning TRUE if it
exists and FALSE otherwise. We can use the following syntax to use this function
intpreg_match(“pattern”,$string);
In the following example we have validated user’s input. First we have declared a form and in
the action page we have validated user’s email.
<body> action.php
</body> ?>
In addition to regular expressions, PHP provides many string functions which can also be used
to evaluate user’s input. Some of the basic string functions are discussed here
strlen(): strlen() function is used to find the length of the string. Following is the syntax of a
regular expression; intstrlen($string);
strcmp(): This function is used to compare two strings. It returns 0 if strings are equal, 1 if first
string is greater and -1 if second is greater. The syntax of the strcmp() function is
– intstrcmp($string1,$string2);
strcasecmp(): strcasecmp() function compares two strings in case insensitive manner.
– strcasecmp($string1,$string2);
strtolower(): It scnverts a string in lower case.
– strtolower($string);
strtoupper(): It converts a string in upper case
– strtoupper($string);
ucfirst(): This function converts the first character of a string to upper case.
– ucfirst($string);
ucwords(): It converts the first character of each word in a string to upper case
– ucfirst($string);
strpos(): It finds the position of the first case-sensitive occurrence of a substring in a string
– strpos($string,sub-string);
strrpos(): It finds the position of the last case-sensitive occurrence of a substring in a string
– strrpos($string,sub-string);
substr_count(): It returns the number of times one string occurs within another
– substr_count($string,sub-string);
Example: In the following example we have taken user’s name, password and re-type password
as input. On the action page, we have used string functions to evaluate user’s input. First we
find the length of the password using strlen() function and if password’s length is less than six
then we have displayed an error message. Similarly, we have compared password and re-type
password and if they don’t match; an error message is displayed. Then we have used
strtolower(), strtoupper(), ucfirst() and ucword() functions on user’s name. Similarly, we have
applied strops(), strrpos() and substr_count() functions on user’s name.
<html> <?php
<head> $name = $_POST['name'];
<title>String Function</title> $pass = $_POST['pass'];
</head> $pass1 = $_POST['pass1'];
<body> if(strlen($pass)<6)
<form id="form1" name="form1" method="post" echo "Too short password";
action="str_action.php"> if(strcmp($pass,$pass1)<>0)
Name: <input type="text" name="name" /> echo "Password mismatch";
Type new password: echostrtolower($name)."<br>";
<input type="text" name="pass" /> echostrtoupper($name)."<br>";
Re-type new password: echoucfirst($name)."<br>";
<input type="text" name="pass1" /> echoucwords($name)."<br>";
<input type="submit" name="Submit value="Submit" /> echostrpos($name,'a')."<br>";
</form> echostrrpos($name,'a')."<br>";
</body> echosubstr_count($name,'a')."<br>";
</html> ?>