Regular expressions are nothing more than a string of characters in a particular order. Regular Expressions (“regex” or “RegExp”) are specifically designed text strings that are used to find patterns in text.
To generate complex expressions, regular expressions use arithmetic operators like (+,-,). They may assist you with activities such as validating email addresses and IP addresses and other text.
Regular expressions are case sensitive by default.
Advantages of PHP Regular Expressions
Because most programming deals with data entries, developers often run into issues when data is collected in free text fields.
- Regular expressions help in the validation of text strings.
- It is used in the searching for a specific string pattern and extraction of matching results.
- By invoking a single function, regular expressions make it easier to find patterns in string data.
- There are built-in regex functions use for identifying patterns.
- Browser detection, spam filtering, password strength checks, and form validations are all common uses for regexes.
PHP Regular Expression Functions
There are some built-in functions for regular expressions in PHP. The following are some of the most commonly used built-in functions:
Function | Description |
preg_match() | This function looks for a pattern within the string and returns true if it is found; otherwise, it returns false. |
preg_match_all() | This function searches the string for all instances of the pattern. It returns the number of times the pattern was detected in the string, which could be 0 as well. |
preg_replace() | This function replaces matching patterns with another string and returns a new string. |
preg_split() | This function works in the same way as to split(), with the exception that it accepts a regular expression as an input parameter for a pattern. It separates the string mostly using a regular expression. |
preg_match()
The preg_match() function looks for a pattern within the string and returns true if it is found; otherwise, it returns false.
<?php $str = "Welcome to Tutorialsart"; $pattern = "/Tutorials/i"; echo preg_match($pattern, $str); ?>
preg_match_all()
The preg_match_all() function searches the string for all instances of the pattern. It returns the number of times the pattern was detected in the string, which could be 0 as well.
<?php $str = "I love my mother and father."; $pattern = "/ther/i"; echo preg_match_all($pattern, $str); ?>
preg_replace()
The preg_replace() function replaces matching patterns with another string and returns a new string.
<?php $str = "Learn C++!"; $pattern = "/C++/i"; echo preg_replace($pattern, "PHP", $str); ?>
preg_split()
The preg_split() function works in the same way as to split(), with the exception that it accepts a regular expression as an input parameter for a pattern. It separates the string mostly using a regular expression.
<?php // Declare a string $str = "Visit! Tutorialsart! and! Learn! PHP"; // Declare a regular expression $regex = "/\!/"; $output = preg_split ($regex, $str); echo "$output[0] <br>"; echo "$output[1] <br>"; echo "$output[2] <br>"; echo "$output[3] <br>"; echo "$output[4] <br>"; ?>
PHP Regular Expression Modifiers
Modifiers can alter the way a search is carried out.
Modifier | Description |
i | It performs a case-insensitive search |
m | It carried out a multiline search |
u | It allows UTF-8 encoded patterns to be correctly matched. |
<?php $str = "Visit Tutorialsart!"; $pattern = "/tutorialsart/"; echo preg_replace($pattern, "Arts", $str); ?>
As in the above example, we have not used a case insensitive modifier that’s why “Tutorialsart” is not replaced by “Art”. Now in the next example, we will use i modifier to make the patter case insensitive.
<?php $str = "Visit Tutorialsart!"; $pattern = "/tutorialsart/i"; echo preg_replace($pattern, "Arts", $str); ?>
Regular Expression Patterns
When used in the context of regular expressions, brackets ([]) have a special meaning. They are used to find a range of characters.
Expressions | Description |
[0-9] | Any decimal digit from 0 to 9 can be matched. |
[a-z] | Any alphabet from a to z can be matched. |
[A-Z] | Any alphabet from A to Z can be matched. |