What is a Regular Expression?
The regular expression is basically started from mathematics which defines a range of characters for some purpose. Different languages get references from this mathematical concept and applied it for searching patterns and validation of data. In this article, you will learn regex in PHP.
There are many use cases of regex in PHP like searching, validating, etc. In this article, you will learn about regular expressions in search patterns. Once you will be able to understand the regex, you can use it anywhere in PHP.
A regular expression can be used to search text from strings or to replace specific characters or strings.
A regular expression can be a single character or combination of some complex patterns.
Syntax of regular expression in PHP
The delimiter can be any character that is not a letter, number, backslash, or space. The most common delimiter is the forward-slash (/), but when your pattern contains forward slashes it is convenient to choose other delimiters such as # or ~.
Generally, regular expression in PHP consists of three parts.
- Delimiter – It specifies that tells the beginning or end of the string in the pattern. A delimiter cannot be a backslash, number, letter, or space. However, a forward slash can be used as a delimiter. If the patterns you are searching for consist of forwarding slashes, you can change the delimiter to tilt ~ or hash #.
- Pattern – It specified the pattern or sequence of characters to search.
- Modifiers (optional) – You can make the matching of pattern cases insensitive or sensitive.
Example
$exp = "/php.org/i";
RegextFunctions in PHP
PHP provides built-in functions for regular expressions. The most common regex functions in PHP are:
- Preg_replace() – It counts the number of times the pattern occurs in the string and return the count.
- Preg_match() – returns 0 in case of pattern not found in the string. 1 in case of found.
- Preg_match_all() – This function replaces the string with another string where the pattern is matched. You will get a more clear idea in the next section if you are finding it hard to understand.
preg_match function in PHP
Preg_match function returns the binary result. That is either 0 or 1. If the string contains the pattern you specify in the first argument of the preg_match function, it will return 1. 0 in case of not found.
Look at the following example, in which we search the string for characters PHP.
<?php
$str = "Visit php.org";
$pattern = "/php/i";
echo preg_match($pattern, $str); // Outputs 1
?>
preg_match_all function in PHP
This function does not tell if the pattern exists in the string or not. It returns the count of the number of times a pattern appears in the string. Look at the following example in which we have performed a case-insensitive search of patterns ing in the given string.
Example
<?php
$str = "It is raining heavily. Cars are slipping outside. ";
$pattern = "/ing/i";
echo preg_match_all($pattern, $str); // Outputs 4
?>
/i in the above example specify that the string can be either in lower case or upper case.
preg_replace function in PHP
preg_replace function returns a new string by replacing the pattern found in the string with some other string of characters. This function accepts three parameters. The first parameter is the pattern to search in the string in the second parameter. Finally, the third parameter is the string to put in place of the pattern found in the string.
Look at the following example, in which we replace the word website with PHP.org
Example
<?php
$str = "Visit Website!";
$pattern = "/Website/i";
echo preg_replace($pattern, "php.org", $str); // Outputs "Visit php.org!"
?>
Regular Expression Modifiers
You can adjust the search behavior of the regex in PHP by defining the following modifier variables.
- I – Used to perform case-insensitive searching.
- U – For the correct searching of utf-8 patterns.
- M – Adds multi-searching in the PHP regex. There are some patterns that search at the beginning or end of each line. So, these patterns look at the beginning and end of each line of the string.
Specify range in PHP regex
Understand the following three concepts. It will help you out to put the range in patterns while searching text or replacing it.
- [abc] – The square brackets indicated that find any one character from these characters in the string.
- [^0-9] – Look for the characters not in the range within the brackets.
- [0-9] – Find any one character in between 0 to 9. 0 and 9 are inclusive in this case.
You can learn more about Regex functions in PHP here