Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Validations php with regex

Regular expressions (regex) are patterns used for matching strings in various programming languages. They utilize metacharacters to define search patterns, character classes for matching specific types of characters, and quantifiers to specify the number of occurrences. The document also includes practical examples of regex usage in PHP for validating usernames, passwords, phone numbers, and email addresses.

Uploaded by

nischealbasnet08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Validations php with regex

Regular expressions (regex) are patterns used for matching strings in various programming languages. They utilize metacharacters to define search patterns, character classes for matching specific types of characters, and quantifiers to specify the number of occurrences. The document also includes practical examples of regex usage in PHP for validating usernames, passwords, phone numbers, and email addresses.

Uploaded by

nischealbasnet08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Regular expression

Regular expressions (shortened as "regex") are special strings representing a pattern to be matched
in a search operation. They are an important tool in a wide variety of computing applications,
from programming languages like Java and Perl, PHP, Python.

The power of regular expressions comes from its use of metacharacters, which are special
characters (or sequences of characters) used to represent something else. For instance, in a regular
expression the metacharacter ^ means "not". So, while "a" means "match lowercase a", "^a"
means "do not match lowercase a".

Note: There are different so-called "flavors" of regex — Java, Perl, and Python have slightly
different rules for regular expressions, for example. On this page, we stick to standard regex, and
you should be able to use this reference for any implementation.

Anchors and Boundaries

Anchors and boundaries allow you to describe text in terms of where it's located. For instance,
you might want to search for a certain word, but only if it's the first word on a line. Or you might
want to look for a certain series of letters, but only if they appear at the very end of a word.
Metacharacter Example
Meaning Example Match
Sequence Expression

^ Start of string or ^abc abc (appearing at start of string or line)


line
$ End of string, or xyz$ xyz (appearing at end of string or line)
end of line

Character Classes

When searching for text, it's useful to be able to choose characters based solely upon their
classification. The fundamental classes of character are "word" characters (such as numbers and
letters) and "non-word" characters (such as spaces and punctuation marks).

Metacharacter Example
Meaning Example Match
Sequence Expression

. Matches any single character except ab.def abcdef, ab9def, ab=def


the newline character.
\s Matches a whitespace character abcd\se abcd e, abcd(tab)e
(such as a space, a tab, a form feed,
etc.)
\S NOT whitespace \S\S\s\S AB D, 99(tab)9
\w A word character. A word character \w\{1,\}-\w\{1,} well-wishes, far-
is a letter, a number, or an fetched
underscore. This set of characters (see quantifiers,
may also be represented by the below)
regex character set [a-zA-Z0-9_]
\W NOT a word character \w\W\{1,\}\w a,!-(?&;b, 9-5

Special Whitespace Characters

Metacharacter Sequence Matches

\n a newline
Metacharacter Sequence Matches

\t a tab
\r a carriage return

\v a vertical tab
\f a form feed

Quantifiers

Quantifiers allow you to declare quantities of data as part of your pattern. For instance, you
might need to match exactly six spaces, or locate every numeric string that is between four and
eight digits in length.

Metacharacter Example
Meaning Example Match
Sequence Expression

* Zero or more of the ca*t cat, ct, caaaaaaaaaaaat


preceding character
character\{m\} Exactly m occurrences f\{3\} fff
of character
character\{m,n\} No fewer than m, but g\{4,6\} gggg, ggggg, gggggg
no more
than n occurrences
of character
character\{m,\} At least m occurrences h\{2,\} hh, hhhhhhhh,
of character and hhhhhhhhhhhhhh would
match, but h would not

Literal Characters and Sequences

Metacharacters are a powerful tool because they have special meaning, but sometimes they need
to be matched literally. For instance, you might need to search for a dollar sign ("$") as part of a
price list, or in a computer program as part of a variable name. Since the dollar sign is a
metacharacter which means "end of line" in regex, you must escape it with a backslash to use it
literally.

Metacharacter Sequence Meaning Example Expression Example Match

\\ Literal backslash \\ \
\^ Literal caret \^\{5\} ^^^^^
\$ Literal dollar sign \$5 $5
\. Literal period Yes\. Yes.
\* Literal asterisk typo\* typo*
\[ Literal open [3\[] 3, [
bracket
\] Literal close \] ]
bracket

Character Sets and Ranges

A character set is an explicit list of the characters that may qualify for a match in a search. A
character set is indicated by enclosing a set of characters in brackets ([and]). For instance, the
character set [abz] will match any of the characters a, b, or z, or a combination of these such
as ab, za, or baz.

Ranges are a type of character set which uses a dash between characters to imply the entire
range of characters between them, as well as the beginning and end characters themselves. For
instance, the range [e-h] would match any of the characters e, f, g, or h, or any combination of
these, such as hef. The range [3-5] would match any of the digits 3, 4, or 5, or a combination of
these such as 45.

When defining a range of characters, you can figure out the exact order in which they appear by
looking at an ASCII character table.
Metacharacter Example
Meaning Example Match
Sequence Expression

[characters] The characters listed inside the [abcd] a, b, c, d, abcd


brackets are part of a matching-
character set
[^...] Characters inside the brackets [^abcd] Any occurrence of any character
are a NON-matching set. Any EXCEPT a, b, c, d. For
character not inside the brackets instance, when, zephyr, e, xyz
is a matching character.
[character- Any character in the range [a-z] Any lowercase letter
character] between two characters,
including the characters, is part
of the set
[^character] Any character that is NOT the [^A] Any character EXCEPT
listed character capital A
Ranges can also be combined by concatenating. [f-hAC-E3- Matches any occurrence of
For instance: 5] an f, g, h, A, C, D, E, 3, 4, 5
Ranges can also be modified with a quantifier. [a-c0-2]* Matches zero or more
For instance: consecutive occurrences
of a, b, c, 0, 1, 2. For
example, ac1cb would match

Grouping

Grouping allows you to treat another expression as a single unit.

Example Example
Metacharacter Sequence Meaning
Expression Match

\(expression\) expression will match as a \(ab\) ab, abracadabra


group
Grouped expressions can be treated as a unit just like a \(ab\)\{3\} abababcdefg
character. For example:
Check for Alphabet : like name

if ( !preg_match ("/^[a-zA-Z\s]+$/",$name)) {
$errors[] = "Name must only contain letters!";
}
or

if (ctype_alpha(str_replace(' ', '', $name)) === false) {


$errors[] = 'Name must contain letters and spaces only';
}

Check Username:

$str = "";
function validate_username($str)
{
$allowed = array(".", "-", "_"); // you can add here more value, you want to allow.
if(ctype_alnum(str_replace($allowed, '', $str ))) {
return $str;
} else {
$str = "Invalid Username";
return $str;
}
}

Or

if (ctype_alnum($username)) {
// Username is valid
}

Or

if(preg_match('/^\w{5,}$/', $username)) { // \w equals "[0-9A-Za-z_]"


// valid username, alphanumeric & longer than or equals 5 chars
}

Or

if(preg_match('/^[a-zA-Z0-9]{5,}$/', $username)) { // for english chars + numbers only


// valid username, alphanumeric & longer than or equals 5 chars
}
function is_valid_username($username){
// accepted username length between 5 and 20
if (preg_match('/^\w{5,20}$/', $username))
return true;
else
return false;
}

function is_valid_password($pwd){
// accepted password length between 5 and 20, start with character.
if (preg_match("/^[a-zA-Z][0-9a-zA-Z_!$@#^&]{5,20}$/", $pwd))
return true;
else
return false;
}

function is_valid_date($date){
//============ date format dd-mm-yyyy
if(preg_match("/^(\d{2})-(\d{2})-(\d{4})$/", $date, $sdate))
if(checkdate($sdate[2], $sdate[1], $sdate[3]))
return true;
}

PHP preg_match example to validate user name

A typical username might have the following requirements.

 It must start with a letter.


 It should have reasonable size. For example greater than 4 and not less than 25.
 It can only contain letters, numbers, and the underscore character.
 And it can not end with an underscore.

The first requirement is to make our pattern start with letters. To accomplish that, we can use the
caret character (^). Which has a special meaning in Regex.

When we use the caret character at the start of a regex pattern, we are telling PHP to match only
strings that start with that letter. Hence, a pattern like the below will match strings starting with
PHP.

$input = "PHP is great";


preg_match('/^PHP/', $input);//Will return true

Similarly, we can use the dollar character ($) to enforce endings in a regex pattern. That means,
$input = "PHP is great";
preg_match('/^PHP$/', $input);//Will return false.

Clearly, the above code returns false because the input string ends with ‘great’ not PHP. To make
it work we can replace that string with ‘great’.

This is great and all. But it still lacks in answering the first requirements. Because we can’t list
all the alphabets and tell PHP to enforce that.

Character classes to specify range

Instead of listing the alphabets, Regex gives us character class ranges. Ranges are awesome
when we want to specify the whole alphabet. Both lowercase and uppercase. Or to check every
number. For example:-

$input = "abc";
preg_match('/^[a-zA-Z]$/', $input);//Will return true.

The above code snippet is a great elaboration of ranges. It checks if the input is made up of
letters. Either small or capital letters. Similarly, we can apply the range, [0-9] to validate our
input for numbers in it.
Awesome. Now we are getting there. We are clearly checking if our user input starts and ends
with letters.

The next requirement is the size of the username. To check for the size we use the format, {n} in
regex. Where n is the number allowed.

Furthermore, we can specify the minimum and maximum value in a brace like this {n, m}.

So, we are ready to handle the second requirement.

$input = "abc2020";
preg_match('/^[a-zA-Z]{4, 25}$/', $input);//Will return true.

Alright, so we are at the third requirement. And it states that our userName can only contain
letters, numbers, and underscore. I think at this point we are capable of doing this without
introducing any new concept. We can use ranges like this, [0-9a-zA-Z_].

Finally, forbidding underscore at the end. For now, we will stick with a range syntax and we
have our final regex with preg_match function like this:-

function validateUserName($userName) {
if(preg_match('/^[a-zA-Z][0-9a-zA-Z_]{2,23}[0-9a-zA-Z]$/', $userName)) {
return true;
}
return false;
}

PHP preg_match to validate US and Ethiopian Phone Numbers

HTML5 forms support different input types. And one of them is the number type. You can use it
to force users to put in only numbers.

The problem is when you want to let your users format the phone number. For example, by
letting them put dash character. In those cases, we can use the character classes and range
limiters we saw above to validated formatted US and Ethiopian phone numbers like this:-

$phoneNumber = "0911-223344";
preg_match('/[0-9]{4}-[0-9]{6}/', $phoneNumber);//Simple regex to validate ethiopian phone
number
preg_match("/[0-9]{3}-[0-9]{3}-[0-9]{4}/", $phoneNumber); // Simple regex to validate US
phone number

I can simplify this pattern by replacing the character class [0-9] with a simple \d. Which simply
indicates any digits.

Strong password validation


The following code snippet validates the password using preg_match() function in PHP with
Regular Expression, to check whether it is strong and difficult to guess.
 Password must be at least 8 characters in length.
 Password must include at least one upper case letter.
 Password must include at least one number.
 Password must include at least one special character.
// Given password
$password = 'user-input-pass';

// Validate password strength


$uppercase = preg_match('/[A-Z]/', $password);
$lowercase = preg_match('/[a-z]/', $password);
$number = preg_match('/[0-9]/', $password);
$specialChars = preg_match('/[^\w]/', $password);

if(!$uppercase || !$lowercase || !$number || !$specialChars || strlen($password) < 8) {


echo 'Password should be at least 8 characters in length and should include at least one upper
case letter, one number, and one special character.';
}else{
echo 'Strong password.';
}
Or

public function checkPassword($pwd, &$errors) {


$errors_init = $errors;

if (strlen($pwd) < 8) {
$errors[] = "Password too short!";
}

if (!preg_match("/[0-9]+/", $pwd)) {
$errors[] = "Password must include at least one number!";
}

if (!preg_match("/[a-zA-Z]+/", $pwd)) {
$errors[] = "Password must include at least one letter!";
}

return ($errors == $errors_init);


}

function validateEmail($email) {

if(filter_var($email, FILTER_VALIDATE_EMAIL)) {

echo "{$email}: A valid email"."<br>";

else {

echo "{$email}: Not a valid email"."<br>";

validateEmail('peter.piper@iana.org');
validateEmail('first.last@example.123');

# php 7.x

<?php

$email_first = 'firstlast11@gmail.com';

$email_second ='firstlast@11gmail,com';

function validateEmail($email) {

$regex = "/^([a-zA-Z0-9\.]+@+[a-zA-Z]+(\.)+[a-zA-Z]{2,3})$/";

echo preg_match($regex, $email) ? "The email is valid"."<br>" :"The email is not valid";

validateEmail($email_first);

validateEmail($email_second);

?>

You might also like