Home » Programming » php » PHP Regular Expressions

PHP Regular Expressions

What is Regex?

Regular expressions (regex) are a sequence of specific characters itself. Generally the regex consists of arithmatic operators & alphanumeric characters to construct specific patterns.

These patterns are widely used for customized validation as per user requirement.

Functions

PHP provides us three functions to deal with regular expressions:

preg_match: The function is used to check pattern match on a string. It returns true when match is found and false if no match is found.

preg_split: The function is used for pattern matching on a string and then splits the results into a numeric array

preg_replace: The function is used to identify a pattern match on a string and replace the matched portion with the specified text.

Below is the general syntax for a regex function such as preg_match, preg_split or preg_replace.

Syntax–

Here,

func_Ebhor” is one of the three functions already described above.

The forward slashes denote the starting and ending of the specific regular expression

Ebhor_regex” is the particular pattern that needs to be matched

Ebhor_txt” is the text string needs to be matched with Ebhor_regex.

Meta characters

The meta characters help us to perform complex pattern matches such as test the validation of an email, telephone, unique username etc. Here is some commonly used meta characters:

Meta CharacterDescription/Example
.Dot matches any single character except a new line character././ matches anything having single character in it
^It matches the beginning of a string./^eb/ matches any string that begins with eb
$Dollar sign matches pattern the end of the string/in$/ matches robin, coffin etc.
*Star matches zero (0) or more characters in the given string./con*/ matches continuous, control, connection etc.
+Plus requires preceding character(s) to appear at least once./ebh+or/ matches ebhor
\Backslash is helpful to escape meta characters./ebhor+\.com/ considers the dot as a literal value during pattern match
[…]Character class/[abc]/ matches abc
a-zIt matches lower case letters./a-z/ matches cool, happy etc.
A-ZIt matches upper case letters like EBHOR, PHP, REGEX etc.
0-9It simply matches any number between 0 and 9./0-3/ matches 0,1,2,3 etc.

Predefined Character Ranges

There are some predefined character ranges, known as character classes, are available in PHP for easier coding.

Character classes contain a collection of specific characters within a range. Some of the most popular character classes are given below:

Sr.NoExpression & Description
1[[:alpha:]] It matches with any string containing alphabetic characters between aA and zZ.
2[[:digit:]] It matches any string containing digits within the range 0 to 9.
3[[:alnum:]] It matches a string containing alphanumeric characters aA-zZ and also 0-9.
4[[:space:]]It matches with any string containing blank space.

Examples

Here is an example of PHP validation using regular expressions:

Managed by MochaHost - Fast & Easy WordPress Hosting