Regular Expressions in PHP

Regular Expressions in PHP

Regular expressions are used to match string patterns mostly they are used to match password, email, and any other user authentication and also use to minimize the danger of SQL injection in PHP.

Previously we discussed this topic in Java and today we will learn some functions and how to make patterns for matching using PHP.

Regular expressions in Java

In PHP preg_match( , ) function is used to match a string with a pattern it takes two parameters and returns some Boolean value like true or false.


The above example shows how to match a pattern with string. In above example, the output is true because the pattern is same as the string and it returns true.

Regex Rules:

In PHP, every regex pattern starts and end with forward slash “/ “.

You can define regex pattern as a string and pass it to preg_match() function.

As in the example above it say the string must conation abc in it is the same format.



Big/Square Brackets:

In regex, square brackets are used to create sets. There are four popular sets that are following:

[a-z] it mean this set contain all the small alphabets from a to z.

[A-Z] it means this set contain all the upper case alphabets from A to Z.

[0-9] it mean this set contain all the digits from 0 to 9.

[a-Z] it mean this set contain all the alphabets from small a to z and also capital A to Z.

We can also make set according to over requirements like [a-f] [1-6] etc.

Mid/Curly Brackets:

The curly brackets use for limits. We can set the limit of the set or character using curly brackets.

Syntax: { min , max }

Example: [a-z] { 1, 8}

The above example shows the set contains all the small alphabets and there must be one or maximum 8 alphabets in the matching string.

Small Brackets:

Small brackets are used to create groups or pattern in the regex.

Example: ([a-z]) ([A-Z])

We discuss group further in detail below.

^ is used for the start and after this the word means the matching string must start with that character. Like if the regex is “\^a\” then this means the matching string must start with a.

$ is used for the end and same act as ^ for the start if it was written in the pattern the matching string must be ending with that character like “\asdv$\” then this mean the string must end with v.

. Dot has some special meaning in regex it means the string contain any character it will be numeric or alphabets or any special character.

+ It means the string must have one 1 or maximum any number of character matches with the character after +.

* This mean the string contain unlimited or zero number also matches with the character after *.

? This mean the string contain the character after? one time or zero time.

i this is use in the end of the pattern to avoid case sensitivity of the string.

| is used as Or like ([a-z]) | ([0-9]) this will or between two groups.


To check and validate email using regex in PHP.

Password with minimum length 8 and maximum any letter and must contain at least one non-alphabets.

  • .* looks for any character 0 or more times.
  • \ is used for starting and ending.
  • [\W]+ matches at least one non-word character
  • {8, } means the pattern must contain at least 8 and at most any number of characters.
  • ^ match the start of the string