PHP Regular Expression - PHP Regex - PHP Tutorial



What is a Regular Expression ?

  • A regular expression is a sequence of characters that forms a search pattern. When you search for data in a text, you can use this search pattern to describe what you are searching for.
  • A regular expression can be a single character, or a more complicated pattern.
  • Regular expressions can be used to perform all types of text search and text replace operations.

Syntax

  • In PHP, regular expressions are strings composed of delimiters, a pattern and optional modifiers.
$exp = "/Wikitechy/i";

Regular Expression Functions

  • 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

preg_match() Function

  • The preg_match() function will tell you whether a string contains matches of a pattern.

Sample Code

<?php
$wiki = "Visit Wikitechy";
$pattern = "/ Wikitechy /i";
echo preg_match($pattern, $wiki);
?>

Output

1

preg_match_all() Function

  • The preg_match_all() function will tell you how many matches were found for a pattern in a string.

Sample Code

<?php
$wiki = "The sun is bright, my heart alight, so life is truly a delight.";
$pattern = "/ight/i";
echo preg_match_all($pattern, $wiki); 
?>

Output

3

reg_replace() Function

  • The preg_replace() function will replace all of the matches of the pattern in a string with another string.

Sample Code

<?php
$wiki = "Visit Kaashiv!";
$pattern = "/kaashiv/i";
echo preg_replace($pattern, "Wikitechy", $wiki);
?>

Output

Visit Wikitechy!

Regular Expression Modifiers

Modifier Description
i Performs a case-insensitive search
m Performs a multiline search (patterns that search for the beginning or end of a string will match the beginning or end of each line)
u Enables correct matching of UTF-8 encoded patterns

Metacharacters

Metacharacter Description
| Find a match for any one of the patterns separated by | as in: cat|dog|fish
. Find just one instance of any character
^ Finds a match as the beginning of a string as in: ^Hello
$ Finds a match at the end of the string as in: World$
\d Find a digit
\s Find a whitespace character
\b Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b
\uxxxx Find the Unicode character specified by the hexadecimal number xxxx

Quantifiers

Quantifiers Description
n+ Matches any string that contains at least one n
n* Matches any string that contains zero or more occurrences of n
n? Matches any string that contains zero or one occurrences of n
n{x} Matches any string that contains a sequence of X n's
n{x,y} Matches any string that contains a sequence of X to Y n's
n{x,} Matches any string that contains a sequence of at least X n's

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.

Sample Code

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

Output

1

Related Searches to PHP Regular Expression - PHP Regex