PHP: Regular expressions
- Ameni OUERTANI
- Nov 21, 2018
- 3 min read
A regular expression, or rational expression, allows you to define a general pattern, called regular expression pattern, by means of particular characters, representative of a set of very varied Strings.

If you define, for example, as a characteristic of a word model that the
first and third letter of a word must be a "p", quantity of words of a
English dictionary meet this criteria, such as pipe, etc. These
patterns allow to search in a text if there are occurrences of a word or
find all the words that meet such a particular criterion, as you have just
see.
They can also make it possible to check if an input made by a visitor of the site is
conform to the commonly expected format, such as that of an e-mail address, and in the
otherwise, display an error message. Without this type of verification, any contact
would be impossible between a customer who identified himself with an address containing an error format and a commercial site that saves email addresses to send
an order confirmation, for example.
You will see successively how to write representative patterns and then the
various functions offered by PHP to perform checks on strings of
characters. In practice, these strings come mostly from inputs
made in a form.
Definition of a basic pattern
Writing regular expression patterns is the most forbidding part of the job
coding. Great care is required in their writings as this conditions the quality
of the result. Patterns are always contained in character strings, and so
in quotation marks, and must be framed by a / (slash) character at the beginning and end
chain.
Search for one or more characters
To find the presence of a particular character, it is sufficient to include it between
single or double quotes. To search for the @ character, you write the model:
$ model = "/ @ /";
To check if a string contains at least one character from a list, you
list all these characters in square brackets.
To search for one or more xyz characters, you have the following reason:
$ model ="/[xyz]/";
With the same syntax, you can define as a pattern an interval of letters or
figures.
For example, if you write:
$ model = "/ [a-z] /";
you are looking for a word of at least one character containing any letters
tiny.
Similarly, the following reason:
$ model = "/ [A-Z] /";
look for any group of capital letters.
The following :
$ model = "/ [0-9] /";
search for the presence of at least one digit between 0 and 9.
There are predefined character classes, which avoid having to create yourself
the sets of characters sought. These classes are summarized in the table below:

The classes of the table are usable like the other reasons by writing them in a
string delimited by quotation marks.
For example, you write the following template:
$ model = "/ [[: alpha:]] /";
To search for lowercase or uppercase alphabetic characters.
The following model:
$ model = "/ [[: digit:]] /";
allows you to search for the presence of numbers.
Finding a precise String
To find the presence of a specific word in a string, create the pattern by writing
the word in quotation marks. The search is validated if the word is exactly in the
chain analyzed and respecting the case.
The following reason:
$ model = "/ Paris /";
check if the word Paris is present in the parsed string. The chain Visit in Paris is
therefore conform to the model but not the chain Visit to Paris.
To detect the presence of at least one of two channels, you must use
the OR logical operator, symbolized by the pipe (|) character placed between the words to search.
The following reason:
$ model = "/ \ com | \ .net /.";
search for .com or .net strings. The address www.php.net is validated but not
www.w3c.org.
Comments