Skip to content

PHP Regular Expressions

PHP is a powerful server-side scripting language that allows developers to create dynamic and interactive web pages. One of its most useful features is its support for regular expressions, which provide a way to match patterns in strings.

What are Regular Expressions

Regular expressions are sequences of characters that define a search pattern. They are used to match and manipulate strings, and can be a powerful tool for validating user input, searching through text, and more.

How Regular Expressions Work in PHP

In PHP, regular expressions are supported through the use of the preg_ family of functions. These functions allow you to perform various operations with regular expressions, including searching, replacing, and splitting strings.

Common preg_ functions

FunctionDescription
preg_match()Searches a string for a match, returns true or false
preg_match_all()Searches a string for all matches, returns the number of matches found
preg_replace()Searches a string for matches and replaces them with a specified string
preg_split()Splits a string into an array based on a regular expression pattern

The Basics of PHP Regular Expressions

The basic syntax of a PHP regular expression is as follows:

PHP regular expression syntax

php
/pattern/modifiers

Where pattern is the sequence of characters to match, and modifiers are optional letters that alter the behavior of the regular expression. Some of the most commonly used modifiers in PHP include:

  • i: Makes the regular expression case-insensitive
  • m: Treats the string as multiple lines
  • s: Treats the string as a single line
  • u: Treats the string as UTF-8 encoded

PHP regular expression to replace text

php
<?php

$text = "Hello World";
$result = preg_replace("/World/", "PHP", $text);
echo $result; // Outputs: Hello PHP

?>

Using PHP Regular Expressions to Validate User Input

One of the most common uses of regular expressions in PHP is to validate user input. For example, you can use a regular expression to ensure that a user-entered email address is in the correct format. The following code demonstrates how this can be done:

PHP regular expression to ensure that a user-entered email address is in the correct format

php
<?php

$email = "[email protected]";
if (preg_match("/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/", $email)) {
    echo "Valid email address";
} else {
    echo "Invalid email address";
}

?>

Note: While this regex works for basic validation, PHP provides the built-in filter_var($email, FILTER_VALIDATE_EMAIL) function for more robust, RFC-compliant email validation.

Advanced Techniques for PHP Regular Expressions

While the basics of PHP regular expressions are straightforward, there are many advanced techniques that you can use to take your skills to the next level. These include:

  • Using capturing groups to extract specific parts of a match
  • Using non-capturing groups to group patterns together
  • Using lookahead and lookbehind assertions to match patterns based on what comes before or after them
  • Using alternation to match multiple patterns
  • Using quantifiers to match a specific number of repetitions of a pattern

PHP regular expression with capturing groups

php
<?php

$text = "Order #12345 placed on 2023-10-01";
if (preg_match("/Order #(\d+)/", $text, $matches)) {
    echo "Order ID: " . $matches[1]; // Outputs: Order ID: 12345
}

?>

Conclusion

PHP regular expressions are a powerful tool that can help you achieve a wide range of tasks. Whether you're validating user input, searching through text, or performing complex string manipulations, regular expressions are an essential part of the PHP developer's toolkit.

Practice

What are the uses of Regular Expressions in PHP as stated in the linked article?

Dual-run preview — compare with live Symfony routes.