PHP preg_match() Function

Learn via video courses
Topics Covered

Overview

preg_match() is a PHP function used for pattern matching using regular expressions. It searches a given string for a match to the regular expression pattern provided and returns a boolean value indicating whether the match was found or not. The function also allows capturing of groups within the matched string. The regular expression pattern is defined using a combination of special characters and predefined patterns that define the search criteria.

Introduction

In PHP, the preg_match function is a powerful tool for working with regular expressions, allowing you to search for specific patterns within a string. Regular expressions, or regex for short, are a set of characters and symbols that define a pattern that can be used to match and manipulate strings. The preg_match function takes two arguments: a regular expression pattern and a string to search within. It then returns an integer value that represents the number of matches found.

The syntax for using preg_match is straightforward. You simply provide the regular expression pattern as the first argument, enclosed in forward slashes, and the string to search within as the second argument.

Regular expressions in preg_match use a combination of characters and special symbols to define search patterns. The regex pattern is a sequence of characters that represents a set of rules to match against the target string. It can include metacharacters, quantifiers, character classes, anchors, and more, allowing for precise pattern matching.

When a match is found using preg_match, you can also extract specific parts of the matched string using capturing groups. Capturing groups are defined within parentheses ( and ) within the regex pattern, allowing you to isolate and retrieve specific portions of the matched text.

Parameters of preg_match in PHP

The preg_match function in PHP takes three parameters: a regular expression pattern, a string to search within, and an optional array to store the matches.

Here is a detailed explanation of each parameter:

Regular Expression Pattern: The first parameter of preg_match is the regular expression pattern that you want to match against the string. It can be any valid regular expression, consisting of a combination of characters, symbols, and modifiers that define a pattern to search for.

String to Search Within: The second parameter of preg_match is the string that you want to search within.

Optional Array to Store Matches: The third parameter of preg_match is an optional array that can be used to store the matches found in the string. This array will be populated with the matched substrings that correspond to any capturing groups defined in the regular expression pattern.

Return Values of preg_match in PHP

The preg_match function in PHP returns an integer value, which represents the number of matches found between the regular expression pattern and the string being searched.

Here are some possible return values of preg_match:

  • 0: This value is returned if no matches are found between the regular expression pattern and the string being searched.
  • 1: This value is returned if a match is found between the regular expression pattern and the string being searched.
  • False: This value is returned if an error occurs during the execution of the preg_match function.

Changelog

The preg_match function in PHP has evolved over the years, with changes and improvements made in each version of the language.

Here are some notable changes and additions made to preg_match in different PHP versions:

  • PHP 5.4.0: In PHP 5.4.0, the preg_match function was modified to support the PREG_OFFSET_CAPTURE flag, which allows you to retrieve the position of each matched substring within the original string.
  • PHP 7.0.0: In PHP 7.0.0, the preg_match function was modified to throw an error if an invalid regular expression pattern was provided as the first parameter.
  • PHP 7.3.0: In PHP 7.3.0, the preg_match function was modified to support the PREG_UNMATCHED_AS_NULL flag, which causes unmatched subpatterns to be returned as null instead of an empty string.
  • PHP 8.0.0: In PHP 8.0.0, the preg_match function was modified to support named capturing groups, which allows you to assign a name to a capturing group in the regular expression pattern.

Exceptions of preg_match in PHP

The preg_match function in PHP does not typically throw exceptions, but it can return false if an error occurs during the execution of the function.

Here are some common error scenarios that can cause preg_match to return false:

  • Invalid Regular Expression: If the regular expression pattern passed as the first parameter to preg_match is invalid, the function will return false. For example, if the pattern contains an unmatched closing parenthesis, preg_match will not be able to parse the pattern and will return false.
  • PCRE Compilation Error: If there is an error during the compilation of the regular expression pattern, preg_match will return false. This can happen if the pattern is too complex or if there is an error in one of the pattern modifiers.
  • PCRE Execution Error: If there is an error during the execution of the regular expression pattern, preg_match will return false. This can happen if the pattern tries to match an invalid character sequence or if there is a problem with the input string being searched.

Examples of preg_match in PHP

Here are some examples of how to use preg_match in PHP:

Example 1: Matching a string against a regular expression pattern

This example searches for the word "brown" within the given string and returns a message indicating whether a match was found or not. Run the above code in your editor for a better and clear explanation.

Example 2: Matching a string against a regular expression pattern with capturing groups

This example searches for the words "brown" and "fox" within the given string and uses capturing groups to extract the matched substrings. The preg_match function populates the $matches array with the matched substrings, which can then be accessed and displayed. Run the above code in your editor for a better and clear explanation.

Example 3: Matching a string against a regular expression pattern with case-insensitivity

This example searches for the word "brown" within the given string, but uses the "i" modifier at the end of the regular expression pattern to make it case-insensitive. As a result, the function matches the word "BROWN" in the string and returns a message indicating that a match was found. Run the above code in your editor for a better and clear explanation.

What is a Regular expression in PHP?

In PHP, a regular expression (or regex) is a sequence of characters that forms a search pattern. It is a powerful tool for matching and manipulating text strings, allowing you to search for specific patterns within a string, replace substrings, and perform other operations.

Regular expressions in PHP are built using a special syntax that defines the pattern to match. The syntax includes a variety of metacharacters and special sequences that represent different types of characters and patterns. , For example,, the dot (.) character represents any character, the caret (^) character represents the beginning of a line, and the dollar sign ($) character represents the end of a line.

Regular expressions provide flexibility and expressiveness, making it possible to match patterns like email addresses, URLs, phone numbers, dates, and more. They can be used for tasks such as data validation, string manipulation, parsing structured or unstructured text, and formatting.

However, regular expressions can be complex, and creating a correct pattern requires a good understanding of their syntax and behaviour. It is important to thoroughly test regular expressions to ensure they behave as expected and handle all possible scenarios.

PHP's regular expression functions allow you to perform powerful operations on strings efficiently. They can greatly simplify tasks that involve searching, extracting, validating, or manipulating text based on specific patterns, providing a flexible and efficient solution for a wide range of string-related operations in PHP.

Why Use Regular Expressions?

Regular expressions (regex) in PHP are a powerful tool for pattern matching and manipulating strings. They provide a concise and flexible way to search, extract, replace, and validate text based on specific patterns.

Here are some reasons why you might use regular expressions in PHP:

  • Pattern Matching: Regular expressions allow you to search for specific patterns within strings. This can be useful for tasks such as finding email addresses, validating phone numbers, identifying URLs, extracting data from structured text, or searching for specific words or phrases.
  • Data Validation: Regular expressions provide a convenient way to validate user input or data against predefined patterns. You can use regex patterns to ensure that data meets specific criteria, such as validating a password strength, checking for the correct format of dates, or verifying that an input matches a specific pattern required by your application.
  • String Manipulation: Regular expressions enable you to perform complex string manipulations efficiently. You can use regex to search and replace text, extract portions of strings, split strings based on patterns, or rearrange data in desired formats. This can be particularly useful when working with large text datasets or when performing bulk operations on strings.
  • Parsing and Extraction: Regular expressions are often used to extract specific information from structured or unstructured text. For example, you can extract data from log files, scrape information from web pages, or parse complex data formats such as CSV or XML using regex patterns.
  • Text Processing and Formatting: Regular expressions provide a powerful mechanism for text processing and formatting. You can use regex to remove unwanted characters or whitespace, format dates or numbers, capitalize or convert text to lowercase, or perform advanced text transformations based on specific patterns.
  • Efficient String Handling: Regular expressions are highly optimized and can efficiently handle string operations. When working with large datasets or performing complex string manipulations, using regex can often be more efficient and faster compared to manually iterating over strings.
  • Flexibility and Expressiveness: Regular expressions offer a concise and expressive syntax for defining complex patterns. They provide a wide range of metacharacters, quantifiers, and special sequences that allow you to define intricate matching rules and conditions.

Regex (Regular Expression) Syntax

Regular expressions in PHP are built using a syntax that consists of a combination of literal characters, metacharacters, and special sequences.

Here is an overview of the syntax used in regular expressions in PHP:

  1. Literal Characters: These are the characters that you want to match exactly. For example, the regular expression /hello/ would match the word "hello" in a string.
  2. Metacharacters: These are special characters that have a special meaning in regular expressions. Some commonly used metacharacters include:
  • .: Matches any single character except a newline.
  • *: Matches zero or more occurrences of the previous character or group.
  • +: Matches one or more occurrences of the previous character or group.
  • ?: Matches zero or one occurrence of the previous character or group.
  • |: Matches either the expression before or after the |.
  • (): Defines a capturing group, which allows you to extract matched substrings.
  1. Special Sequences: These are shorthand sequences that represent commonly used character classes or patterns. Some commonly used special sequences include:
  • \d: Matches any digit (0-9).
  • \s: Matches any whitespace character (space, tab, newline, etc.).
  • \w: Matches any word character (a-z, A-Z, 0-9, and underscore).
  • \b: Matches a word boundary.
  • \A: Matches the start of a string.

Explaining the pattern "[ ^ [a-zA-Z0-9._-] +@[a-zA-Z0-9-]+.[a-zA-Z.]{2,5}$/]

The pattern [ ^ [a-zA-Z0-9._-] +@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/] is a regular expression used in PHP to validate email addresses.

Let's break down the different components of the pattern:

  • [ ]: Square brackets are used to define a character set, which matches any one character in the set.
  • ^: A caret inside a character set means "not". In this case, it means "not any of the following characters".
  • a-zA-Z0-9._-: This character set matches any alphanumeric character, dot, underscore, or hyphen.
  • +: The plus sign after the character set means that one or more of the preceding character or character set must be present.
  • @: Matches the "@" symbol, which is required in an email address.
  • [a-zA-Z0-9-]+: Matches one or more alphanumeric characters or hyphens in the domain name (after the "@").
  • \.: Matches a literal period (".") in the domain name.
  • [a-zA-Z.]{2,5}: Matches two to five letters or periods in the domain name. This allows for top-level domains such as ".com" or ".co.uk".
  • $: Matches the end of the string, ensuring that the email address is complete.

Conclusion

  • preg_match is a PHP function that allows you to search for a pattern within a string using a regular expression.
  • preg_match takes three parameters: the regular expression pattern, the string to search, and an optional array for capturing matched substrings.
  • The return value of preg_match is an integer indicating the number of matches found.
  • preg_match is a powerful tool for working with text data in PHP, providing flexibility, efficiency, consistency, validation, and extraction capabilities.
  • The syntax used in regular expressions in PHP consists of a combination of literal characters, metacharacters, and special sequences.