Regular Expressions
Regular expression is a sequence of characters that forms a search pattern. PHP regular expression functions are mentioned below:
- preg_filter()
- preg_grep()
- preg_last_error()
- preg_match()
- preg_match_all()
- preg_replace()
- preg_replace_callback()
- preg_replace_callback_array()
- preg_split()
- preg_quote()
Regular Expression Modifiers
i |
for case-insensitive search |
m |
for multi-line search |
u |
enables correct matching of UTF-8 encoded pattern |
Regular Expression Patterns
[abc] |
searches one character ranging from within the brackets |
[^abc] |
searches for a character which is not in the brackets |
[0-9] |
searches one character ranging from 0 to 9 |
Meta Characters
| |
start of alternative branch |
. |
searches any character except for newline |
^ |
defined as the beginning of the string. For example : ^Good |
$ |
defined as the end of the string. For example : Good$ |
\d |
searches a digit |
\s |
searches a whitespace character |
\ |
general escape character |
- |
character range |
( |
subpattern start |
) |
subpattern end |
{ |
min/max quantifier start |
} |
min/max quantifier end |
[ |
character class definition start |
] |
character class definition end |
Quantifiers
Repetition is specified by the quantifiers. Three commonly used quantifiers are:
* |
equivalent to {0,} matches for 0 or more occurrences of pattern |
+ |
equivalent to {1,} matches for at least 1 occurrence of pattern |
? |
equivalent to {0,1} matches for 0 or 1 occurrences of pattern |
Regular Expression Methods explanation & code examples
preg_filter()
method
This method returns a string or an array of strings where matches of the pattern have been replaced with the replacement string.
It is quite similar to preg_replace()
. However, in preg_filter()
if a pattern is not found in string then that string will not be used in return value and the function returns NULL
.
//syntax
preg_filter(pattern, replacement, string, limit, count);
Syntax understanding
pattern
: A regular expression or an array of regular expressions. - mandatory.
replacement
: A replacement string or an array of replacement strings. - mandatory
string
: An input string or an array input string. - mandatory
limit
: Default is -1 means unlimited. To specify a limit on how many replacements can be done in each string. - optional
count
: A variable which indicates how many replacements were made, after the function execution. - optional
Code example using preg_filter()
preg_grep()
method
It returns an array containing the elements who got matched with the pattern.
//syntax
preg_grep(pattern, string, flag);
Syntax understanding
pattern
: A regular expression helps to indicate what to search. - mandatory
string
: An array of strings. - mandatory
flag
: There is only one flag for this function. Passing the constant PREG_GREP_INVERT
will make the function return only items that do not match the pattern. - optional
Code example using preg_grep()
preg_last_error()
method
It returns an error code for a regular expression being evaluated.
PREG_NO_ERROR
No error
PREG_INTERNAL_ERROR
Error in evaluation
PREG_BAD_UTF8_ERROR
Invalid UTF-8 error
//syntax
preg_last_error();
Code example using preg_last_error()
preg_match()
method
It searches for the first match of a pattern in a string.
//syntax
preg_match(pattern, string, matches, flag, offset);
Syntax understanding
pattern
: The pattern to search for. - required
string
: Input array of strings. - required
matches
: An array filled with the matches of result. - optional
flag
: An options to define the way, how the matches array will be structured. - optional. Possible values are PREG_OFFSET_CAPTURE
& PREG_UNMATCHED_AS_NULL
.
PREG_OFFSET_CAPTURE
: Every match will be an array, where the first element is the substring containing the match and second is the position of that element.
PREG_UNMATCHED_AS_NULL
: Unmatched subpatterns will be returned as NULL
instead of an empty string.
offset
: To specify where to start the search. Default value is 0. - optional
Code example using preg_match()
preg_match_all()
method
It helps to perform a search for all the possible matches of a pattern in a string.
//syntax
preg_match_all(pattern, string, matches, flag, offset);
Syntax understanding
pattern
: The pattern to search for. - required
string
: Input array of strings. - required
matches
: An array filled with the matches of result. - optional
flag
: Options to define the way, how the matches array will be structured. Optional. Possible values are PREG_OFFSET_CAPTURE
& PREG_UNMATCHED_AS_NULL
.
PREG_OFFSET_CAPTURE
: Every match will be an array,where the first element is the substring containing the match and second is the position of that element.
PREG_UNMATCHED_AS_NULL
: Unmatched subpatterns will be returned as NULL
instead of an empty string.
One of the following structures may be selected:
PREG_PATTERN_ORDER
: Default. Orders results as such matches[0]
is an array full of pattern matches, $matches[1]
is an array of strings matched by the first parenthesized subpattern and so on.
PREG_SET_ORDER
: Orders results as such matches[0]
is an array of the first set of matches and matches[1]
is an array of second set of matches, and so on.
offset
: To specify where to start the search. Default value is 0. - optional
Code example using preg_match_all()
preg_replace()
method
Returns a string or an array of strings where all the resultant matches of a pattern or the list of patterns found in a string are replaced with the substrings.
Three ways to use this function are:
- A single pattern and a single replacement string.
- An array of patterns and a single replacement string.
- An array of patterns and an array of replacement strings. Match of each pattern is replaced with the replacement string at the same position in the replacement string array.
//syntax
preg_replace(pattern, replacement, string, limit, count);
Syntax understanding
pattern
: A regular expression or an array of regular expressions. - required
replacement
: A replacement string or an array of replacement strings. - required
string
: An input string or an array input string. - required
limit
: Default is -1 means unlimited. To specify a limit on how many replacements can be done in each string. - optional
count
: A variable which indicates how many replacements were made, after the function execution. - optional.
Code example using preg_replace()
preg_replace_callback()
method
Returns a string where all the matches of the expression got replaced with the substring returned by the callback function.
//syntax
preg_replace_callback(pattern, replacement, string, limit, count);
Syntax understanding
pattern
: A regular expression or an array of regular expressions. - required
replacement
: A callback function which returns the replacement where one parameter contains an array of matches. The first element in the array contains the match for the whole expression while the other elements contain matches for each of the groups in the expression. - required
string
: An input string or an array input string. - required
limit
: Default is -1 means unlimited. To specify a limit on how many replacements can be done in each string. - optional
count
: A variable which indicates how many replacements were made, after the function execution. - optional.
Code example using preg_replace_callback()
preg_replace_callback_array()
method
Returns a string or an array of strings in which the matches of a set of regular expressions are replaced with the return value of a callback function.
//syntax
preg_replace_callback_array(pattern, string, limit, count);
Syntax understanding
pattern
: An associative array associating regular expression patterns to callback functions having one argument as an array of matches.The first element in the array contains the match for the whole expression while the remaining elements have matches for each of the groups in the expression. - required
string
: An input string or an array input string. - required
limit
: Default is -1 means unlimited. To specify a limit on how many replacements can be done in each string. - optional
count
: A variable which indicates how many replacements were made, after the function execution. - optional
Code example using preg_replace_callback_array()
preg_split()
method
Splits a string into an array using matches of a regular expression as separators.
//syntax
preg_split(pattern, string, limit, flag);
Syntax understanding
pattern
: A regular expression specifying what to use as a separator. - required
string
: An input string. - required
limit
: Default is - 1, means unlimited. To specify a limit on how many elements a returned array can have. - optional
flag
: Options to change the returned array. - optional
PREG_SPLIT_NO_EMPTY
: removes empty strings from the returned array.
PREG_SPLIT_DELIM_CAPTURE
: If the regular expression contains a group wrapped in parentheses then the matches of this group will be included in the returned array.
PREG_SPLIT_OFFSET_CAPTURE
: Returned array will be an array with two elements, the first element is the substring and the second element is the position of the first character of the substring in the input string.
Code example using preg_split()
preg_quote()
method
Adds a backslash to the characters having a special meaning in regular expressions.
//syntax
preg_quote(string, delimiter);
Syntax understanding
string
: An input array of strings. - required
delimiter
: An optional parameter. Default value is NULL. Delimiter or a character to be used in regular expression.
Code example using preg_quote()