PHP Regular Expressions


What is a Regular Expression

A regular expression is a search pattern made up of a series of characters. This search pattern can be used to define what you're looking for while searching for data in a text.

A regular expression is a pattern that can be simple as single letter or as complex pattern.

All forms of text search and replace operations may be performed with regular expressions.

Syntax :- Strings made of delimiter characters, a pattern and optional modifiers make up a regular expression in the PHP language.

$exp = "/w3schools/i";

In the previous example, / is the delimiter, simmanchith is the pattern searched for and i is a search insensitive modifier.

Any character that isn't a letter, number, backslash, or space can be used as a delimiter. The forward-slash(/) is the most frequent delimiter, although if your pattern incorporates forward slashes, alternative delimiters such as # or ~ are more useful.

You can also search for these topics, regular expression in php, validate the php regular expression, regular expression for email using php, php regular expression alphanumeric and space, how to replace and match the regular expression using php, syntax for php regular expression, php delimiter can be any character with regular expression.

Regular Expression Functions

Regular expressions are supported by a number of PHP functions. The methods preg_match(), preg_match_all(), and preg_replace() are among the most regularly utilised :

Function Description
preg_match() If the pattern was discovered in the string, 1 is returned; otherwise, 0 is returned.
preg_match_all() If the pattern was detected just once in the string, it will return0; otherwise, it will return 1.
preg_replace() Replacing of pattern-matching strings with another string is returned.
You can also search for these topics, php regular expression functions, list the type of regular expression functions in php, php variety of functions with regular expression functions, define the php regular expression functions and description.

Using preg_match()

The preg_match() method will notify you if a string includes pattern matches.

Example :- To search for "simmanchith" in a string without regard to case, use a regular expression:

<?php
$str = "Visit Simmanchith.com";
$pattern = "/simmanchith/i";
echo preg_match($pattern, $str); 
?>

Output :-

1

You can also search for these topics, multiple pattern using php preg_match(), preg_match case insensitive in php, define the special character in preg_match function, Example for php preg_match() function, php preg_match numbers only.

preg_match_all()

The preg_match_all() method returns the number of matches found in a string for a given pattern.

Example :- To count the number of instances of "ain" in a string using a regular expression, perform the following :

<?php
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern = "/ain/i";
echo preg_match_all($pattern, $str);
?>

Output :-

4

You can also search for these topics, multiple pattern using php preg_match_all(), preg_match_all case insensitive in php, define the href url in preg_match_all function, Example for php preg_match_all function, php preg_match_all html tags.

Using preg_replace()

The preg_replace() method will substitute or replace a string for change all matching patterns.

Example :- To replace Microsoft with simmanchith in a string, use a casual regular expression :

<?php
$str = "Visit Microsoft!";
$pattern = "/microsoft/i";
echo preg_replace($pattern, "simmanchith", $str);
?>

Output :-

Visit simmanchith!

You can also search for these topics, php preg_replace(), preg_replace_callback in php, define the pattern string using php preg_replace(), how to matchall the php preg_replace() function, define the special character in preg_replace function, php preg_replace numbers only, Example for php using preg_replace().

Regular Expression Modifiers

Modifiers can alter the way a search is carried out.

Modifier Description
i case-insensitive search is performed.
m A multiline search is performed (Beginning and ending patterns will match the beginning and ending of every line).
u Allows UTF-8 encoded patterns to be correctly matched.
You can also search for these topics, php Regular Expression Modifiers, how to change the regular expression modifiers in php, define the modifiers with regular expression in php, php modifiers and description with regular expression.

Regular Expression Patterns

Brackets are used to find for a specific set of characters :

Expression Description
[abc] Between the brackets, choose one character from the list.
[^abc] Look for any character that isn't in the brackets.
[0-9] character can you find in the range of 0 to 9.
You can also search for these topics, php Regular Expression patterns, how to find the rangr of regular expression modifiers in php, define the patterns with regular expression in php, php Expressions and description with regular expression.

Metacharacters

Metacharacters are characters that have a specific significance :

Metacharacter Description
| Look for a match for any of the patterns separated by | , such as cat|dog|fish.
. Find only one character in each category.
^ When a string begins with, it is matched ex: ^Hello.
$ Searches for a match at the end of a string: like World$
\d Search a digit
\s Search a whitespace character
\b Look for a match at the start of a word like this: b\WORD, or at the ending of a phrase such as this: WORD\b
You can also search for these topics, php metacharacters, description for php metacharacters, list out the metacharacters in php, php filter the metacharacters using php.

Quantifiers

define the quantities of Quantifiers :

Quantifier Description
n+ Any string with at least one n will be matched.
n* Any string with zero or more instances of n is matched.
n? Any string with zero or one instances of n is matched.
n{x} Any string with a series of X n's will be matched.
n{x,y} Any string with a sequence of X to Y n's will be matched.
n{x,} Any string containing at least X n's will be matched.

Note : You can use a back slash (\) to escape your phrase when you have to search for one of the special letters. For example, the following phrase may be used to search for one or more question marks : $pattern = '/?+/';

You can also search for these topics, define the quantities of php quantifiers, patterns in php quantifiers, list the php quantifiers, regular expression php quantifiers.

Grouping

To apply quantifiers to whole patterns, use parentheses ( ). They can also be used to choose which elements of the pattern to match.

Example :- Search for the word "banana" by searching for followed by two cases :

<?php
$str = "Apples and bananas.";
$pattern = "/ba(na){2}/i";
echo preg_match($pattern, $str);
?>

Output :-

1

You can also search for these topics, php grouping array, how to grouping the php function, patterns in php grouping, define the regular expression grouping in php, value the grouping in php, how to use namespaces in php grouping.