codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

26 / 56 topics
24Strings in PHP25String Functions26Regular Expressions in PHP
Tutorials/PHP/Regular Expressions in PHP
🐘PHP

Regular Expressions in PHP

Updated 2026-05-15
10 min read

Regular Expressions in PHP

Introduction

Regular expressions, often abbreviated as regex or regexp, are a powerful tool for pattern matching and text processing. They allow you to search, replace, split, and validate strings based on specific patterns. In PHP, regular expressions are handled by a set of functions that provide extensive capabilities for working with text.

In this tutorial, we will explore how to use regular expressions in PHP to perform various string operations such as searching, replacing, splitting, and validating text. We'll cover the basics of regex syntax and then dive into practical examples to illustrate how these concepts can be applied in real-world scenarios.

Concept

Regular expressions are defined by a pattern that describes a set of strings. The pattern is used to match or search for substrings within a larger string. PHP provides several functions to work with regular expressions, including:

  • preg_match(): Checks if a pattern matches a string.
  • preg_replace(): Replaces occurrences of a pattern in a string.
  • preg_split(): Splits a string by a pattern.
  • preg_grep(): Filters an array using a regular expression.

Basic Syntax

Regular expressions use a variety of special characters and metacharacters to define patterns. Here are some common elements:

  • Literal Characters: Match the exact character, e.g., a matches the letter "a".
  • Character Classes: Define a set of characters to match, e.g., [abc] matches any of "a", "b", or "c".
  • Quantifiers: Specify how many times a pattern should be matched, e.g., * (zero or more), + (one or more), ? (zero or one).
  • Anchors: Match specific positions in the string, e.g., ^ (start of the string), $ (end of the string).

Examples

1. Searching with preg_match()

The preg_match() function checks if a pattern matches a given string and returns the number of matches found.

php
1<?php
2$pattern = '/hello/';
3$subject = 'Hello, world!';
4if (preg_match($pattern, $subject)) {
5 echo "Pattern matched!";
6} else {
7 echo "Pattern not matched.";
8}
9?>
Terminal
$ php script.php
Output
Pattern matched!

2. Replacing with preg_replace()

The preg_replace() function replaces occurrences of a pattern in a string with a replacement string.

php
1<?php
2$pattern = '/world/';
3$replacement = 'PHP';
4$subject = 'Hello, world!';
5echo preg_replace($pattern, $replacement, $subject);
6?>
Terminal
$ php script.php
Output
Hello, PHP!

3. Splitting with preg_split()

The preg_split() function splits a string by a pattern and returns an array of substrings.

php
1<?php
2$pattern = '/s+/';
3$subject = 'Split this sentence into words.';
4print_r(preg_split($pattern, $subject));
5?>
Terminal
$ php script.php
Output
Array
(
  [0] => Split
  [1] => this
  [2] => sentence
  [3] => into
  [4] => words.
)

4. Validating with preg_match()

Regular expressions can also be used to validate input, such as checking if a string is a valid email address.

php
1<?php
2$pattern = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/';
3$email = 'example@example.com';
4if (preg_match($pattern, $email)) {
5 echo "Valid email address!";
6} else {
7 echo "Invalid email address.";
8}
9?>
Terminal
$ php script.php
Output
Valid email address!

What's Next?

In the next section, we will explore how to work with dates and times in PHP. This includes creating, manipulating, and formatting date objects.

Stay tuned for more tutorials on PHP and other programming topics at codingstuff.io!


PreviousString FunctionsNext Date and Time in PHP

Recommended Gear

String FunctionsDate and Time in PHP