PHP | Regular Expressions
Tags: beginners, tutorial, programming, javascript
Published: 2022-09-22 14:35:05

## Introduction

## Introduction

Most programming languages involves data entry, developers frequently run into issues when data is gathered in free text fields. In today's application programming, regular expressions are practically ubiquitous.

A popular open-source scripting language called PHP.  Text, HTML, CSS, JavaScript, and PHP code can all be found in PHP files.

When searching for or replacing a string with a pattern, developers frequently use regular expressions, which are a fundamental component of PHP.

This search pattern can be used to specify your search criteria when looking for information within a text. A regular expression can be a simple pattern or just one character.
All kinds of text search and text replacement operations can be carried out using regular expressions.

This lesson will teach you how regular expressions function as well as how to effectively use them in PHP for pattern matching.

Let's dive into the world of regular expressions now, if you're ready.

## What are Regular Expressions

Regular Expressions, also referred to as "regex" or "RegExp," are formatted text strings that are used to search for patterns in text. A search pattern is made up of a string of characters.

An efficient technique to describe a string pattern that matches a specific amount of text is through the use of regular expressions. You probably already know that PHP, an open-source language frequently used for building websites, offers regular expression functions as a crucial tool. 
Numerous other programming languages use regular expressions in a similar way to PHP. The same is true for other apps that offer regexes with different syntaxes. Regexes are used on huge files and strings by numerous contemporary languages and technologies.

One of the most potent techniques for effective and efficient text processing and manipulation nowadays is the use of regular expressions. For instance, it can be used to find or replace matching strings within text content, check the accuracy of data format (such as name, email, phone number, etc.) entered by the user, and more.

They merely define a specific search pattern as a text string and are nothing more than a pattern or a series of characters. They lay the groundwork for capabilities related to pattern matching.

You can replace one string with another string, split a string into multiple chunks, and search for a certain string inside another string using regular expression.

You can look for a specific string inside another string using regular expression. We can also split a string into many parts and replace one string with another string. They construct complex expressions by using the arithmetic operators
(+, -, ^ ). They may assist you with things like validating IP addresses and email addresses, among others.



##  Advantages  of Regular Expressions

 Here are some benefits.


1. Regular expressions aid programmers in text string validation.
2. It provides a potent tool for pattern recognition, analysis, and text string modification.
3. Simple ways are offered to find the patterns by employing regexes functions.
4. Regexes aid in the development of the HTML tag recognition system.
5. Regexes are frequently used for spam filtration, password strength verification, form validation, and browser detection.
6. It is useful in user input validation testing for user inputs such as IP address, email address, and mobile number.
7. It assists in emphasizing the file's unique keywords based on the input or search result.
8. Metacharacters let us design more intricate patterns
9.  Browser detection, spam filtration, determining the strength of passwords, and form validations are the main uses of regexes.


## Operators in Regular Expressions

Let us look into some of the operators in PHP regular expressions.

| ##     Operator | ## Description                                                                                |
| --------------- | --------------------------------------------------------------------------------------------- |
| ^               | It denotes the start of the string.                                                           |
| $               | It denotes the end of the string.                                                             |
| .               | It denotes almost any single character.                                                       |
| ()              | It denotes a group of expressions.                                                            |
| []              | It finds a range of characters for example [xyz] means x, y, or z.                            |
| [^]             | It finds the items which are not in range for example [^abc] meas NOT a, ,b or c.             |
| – (dash)        | It finds for character range within the given item range for example [a-z] means a through z. |
| | (pipe)        | It is the logical OR for example x | y means x OR y.                                          |
| ?               | It denotes zero or one of preceding character or item range.                                  |
| *               | It denotes zero or more the of preceding character or item range.                             |
| +               | It denotes one or more of preceding character or item range.                                  |
| {n}             | It denotes exactly n times of preceding character or item range for example n{2}.             |
| {n, }           | It denotes atleast n times of preceding character or item range for example n{2, }.           |
| {n, m}          | It denotes at least n but not more than m times for example n{2, 4} means 2 to 4 of n.        |
| \               | It denotes the escape character.                                                              |
|                 |                                                                                               |
|                 |                                                                                               |



## Create Regular Expressions

To create a regular expression, you place a pattern in forward-slashes like this:

    '/pattern/';

For example:

    <?php
    
    $pattern = '/\d+/';


| Function         | Description                                                                          |
| ---------------- | ------------------------------------------------------------------------------------ |
| preg_match()     | Returns 1 if the pattern was found in the string and 0 if not                        |
| preg_match_all() | Returns the number of times the pattern was found in the string, which may also be 0 |
| preg_replace()   | Returns a new string where matched patterns have been replaced with another string   |

A number with one or more digits is also matched by the '$pattern' string using a regular expression. For instance, 1, 40, 800, etc. are compatible.

Forward slashes serve as separators. The delimiters can be any of the following characters: "," "!", "@," "#," "$," or any of the brackets "," "()," "[]," or ">."

In some circumstances, braces make regular expressions easier to interpret.
Keep in mind that delimiters such as alphanumeric, multi-byte, and backslashes (") are not permitted.

## Utilizing Regular Expressions to Find Strings

Regular expressions can be used with a number of PHP functions. When looking for patterns in strings, "preg match()" and "preg match all()" are among the most often used functions.

******preg match() function in PHP**
The 'preg match()' method is used to perform a search based on a regular expression. For instance:

    <?php
    
    $pattern = '{\d+}';
    $text = 'HTML 5 was released on November 22, 2008';
    
    if (preg_match($pattern, $text)) {
        echo "match";
    } else {
        echo "not match";
    }

output:


    match

Preg match() looks for matches to the $pattern in the $text.
Preg match() returns a "1" if there is a match in the $text, a "0" if there isn't, or a "false" if it fails.
The third parameter is added to the "preg match()" method to obtain the text that matches the pattern, like in the following example:


    <?php
    
    $pattern = '{\d+}';
    $text = 'PHP 8 was released on November 26, 2020';
    
    if (preg_match($pattern, $text, $matches)) {
        print_r($matches);
    }

output:

    Array
    (
         [0] => 8
         [1] => 26
         [2] => 2020
            )
    )
    

All of the matches are contained in the '$matches' parameter. The text that matches the pattern is kept in the "$matches[0]" variable. It is the number 8 in this instance.

The texts that match the first and second are stored in the arrays "$matches[1]," "$matches[2]," etc.
Preg match() only returns the first match and terminates its search as soon as it does. You can use the "preg match all()" function to find all matches.


**PHP preg_match_all() function**
The `[preg_match_all()](https://www.phptutorial.net/php-tutorial/php-preg_match_all/)` function searches for all matches to a regular expression. For example:


    <?php
    
    $pattern = '{\d+}';
    $text = 'HTML 5 was released on January 22, 2008';
    
    if (preg_match_all($pattern, $text, $matches)) {
        print_r($matches);
    }

output:


    Array
    (
        [0] => Array
            (
                [0] => 5
                [1] => 22
                [2] => 2008
            )
    )

The "preg match all()" function in this example places all matches in a multidimensional array with the first element being the strings ("5," "22," and "2008" in this case) that match the pattern.
The number of matches, which can be 0 or a positive integer, is returned by the 'preg match all()' function.


******Regular expressions are used to replace strings.**

'preg replace()' is a function that you can use to replace strings that match a regular expression. For instance:

    <?php
    
    $pattern = '/\d+/';
    $text = 'PHP 8 was released on 11/26/2020';
    
    echo preg_replace($pattern, '%d', $text);


output:

    PHP %d was released on %d/%d/%d

In the  example above, the `preg_replace()` function replaces all numbers in the $text with the string `%d`.


## **PHP offers two sets of regular expression functions:**
1. POSIX Regular Expression
2. PERL Style Regular Expression


## POSIX Regular Expression

Similar to how numerous operators or elements are joined to create more complicated expressions in arithmetic, POSIX regular expressions have a structure.
A regular expression that only looks for single characters within a string is the simplest kind. For instance, "g" inside the cage string or toggle. Here are some terms that will be used in POSIX regular expressions:


## **Brackets**

When employed in regular expressions, brackets [] have a specific meaning. These are used to determine the character range contained therein.


| Expression | Description                                               |
| ---------- | --------------------------------------------------------- |
| [0-9]      | It matches any decimal digit 0 to 9.                      |
| [a-z]      | It matches any lowercase character from a to z.           |
| [A-Z]      | It matches any uppercase character from A to Z.           |
| [a-Z]      | It matches any character from lowercase a to uppercase Z. |

These price ranges are frequently employed. You can use the range values to match any decimal digit between 0 and 6, for example, [0-6].



## Quantifiers

A special character can be used to indicate the positions of bracketed character sequences and single characters. Every every character has a unique meaning. There is a specific sequence for the characters +, *,?, $, and "int range" flags.

| Expression | Description                                                    |
| ---------- | -------------------------------------------------------------- |
| **p+**     | It matches any string that contains atleast one p.             |
| **p***     | It matches any string that contains one or more p's.           |
| **p?**     | It matches any string that has zero or one p's.                |
| **p{N}**   | It matches any string that has a sequence of N p's.            |
| **p{2,3}** | It matches any string that has a sequence of two or three p's. |
| **p{2, }** | It matches any string that contains atleast two p's.           |
| **p$**     | It matches any string that contains p at the end of it.        |
| **^p**     | It matches any string that has p at the start of it.           |

 PHP currently provides seven functions to search strings using POSIX-style  regular expression 

| Function             | Description                                                                                                                                                                              |  |
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |  |
| **preg_match()**     | If the pattern is present, this function looks for it inside the string and returns true; otherwise, it returns false.                                                                   |  |
| **preg_match_all()** | All instances of the pattern in the string are matched by this function.                                                                                                                 |  |
| **preg_replace()**   | Similar to the ereg replace() method, the preg replace() function also allows for search and replace using regular expressions.                                                          |  |
| **preg_split()**     | The only difference between this function and split() is that this one allows regular expressions as input parameters for pattern. The string is mostly divided by a regular expression. |  |
| **preg_grep()**      | Preg grep() locates every element in input array and returns the elements of the array that match the regexp (relational expression) pattern.                                            |  |
| **preg_quote()**     | the characters used in the regular expression.                                                                                                                                           |  |



## PERL Style Regular Expression

Regular expressions written in Perl are very comparable to POSIX. Regular expression functions written in the Perl manner can be used interchangeably with the POSIX syntax. The quantifiers presented in the POSIX section can also be used in regular expressions written in the PERL language.

## Metacharacters

An alphabetical character followed by a backslash is referred to as a metacharacter and has a specific meaning.
**For example** - '\d' metacharacter can be used search large money sums: /([\d]+)000/. Here /d will search the string of numerical character.
Below is the list of metacharacters that can be used in PERL Style Regular Expressions -

| Character         | Description                                                 |
| ----------------- | ----------------------------------------------------------- |
| **.**             | Matches a single character                                  |
| **\s**            | It matches a whitespace character like space, newline, tab. |
| **\S**            | Non-whitespace character                                    |
| **\d**            | It matches any digit from 0 to 9.                           |
| **\D**            | Matches a non-digit character.                              |
| **\w**            | Matches for a word character such as - a-z, A-Z, 0-9, _     |
| **\W**            | Matches a non-word character.                               |
| **[aeiou]**       | It matches any single character in the given set.           |
| **[^aeiou]**      | It matches any single character except the given set.       |
| **(foo|baz|bar)** | Matches any of the alternatives specified.                  |

## **Modifiers**

A regular expression greatly simplifies the process because there are numerous modifiers accessible. Consider case-sensitivity or searching across many lines, for instance.
The list of modifiers used in PERL Style Regular Expressions is provided below.

| Character | Description                                                                                                                                                          |
| --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **i**     | Makes case insensitive search                                                                                                                                        |
| **m**     | It specifies that if a string has a carriage return or newline characters, the $ and ^ operator will match against a newline boundary rather than a string boundary. |
| **o**     | Evaluates the expression only once                                                                                                                                   |
| **s**     | It allows the use of .(dot) to match a newline character                                                                                                             |
| **x**     | This modifier allows us to use whitespace in expression for clarity.                                                                                                 |
| **g**     | It globally searches all matches.                                                                                                                                    |
| **cg**    | It allows the search to continue even after the global match fails.                                                                                                  |

## **Grouping**

You can use parentheses `( )` to apply quantifiers to entire patterns. They also can be used to select parts of the pattern to be used as a match.
**Example**
Use grouping to search for the word "orange" by looking for ‘or’ followed by two instances of ‘*an’*:
 

    <?php
    $str = "Apples and orange.";
    $pattern = "/or(an){2}/i";
    echo preg_match($pattern, $str); 
     

 
  Outputs:

     1



# Conclusion

Finally, we have come to the completion of this tutorial and hopefully, you got value from it.
Thanks for reading, please hit the like, clap, or heart button to show some love, I will see you in the next tutorial…

We have leant a lot about  PHP Regular Expressions.

If you have any question, please leave it on the comments. Like and share, and till next time, all the best!


# About the Author

I am Jenifer Eze, an enthusiastic developer with a passion for JavaScript, PHP, HTML & CSS.

I work as a freelancer, building websites for clients, and love writing technical tutorials to teach others what I do. I am eager to hear from you. Reach me on [LinkedIn](https://www.linkedin.com/in/jennifer-eze-969272239/), [Github](https://github.com/nnekajenny), or my [website](https://nnekajenny.github.io/).