Using Regular Expressions
Match and manipulate strings with regular expressions.
Using Regular Expressions is a free PHP Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Using Regular Expressions
Welcome to the lesson on using regular expressions! In this lesson, you’ll learn how to use PHP’s regular expression functions to match patterns in strings and validate data. Let’s dive in!

2
What Are Regular Expressions?
Regular expressions (regex) are patterns used to match strings. They are widely used for:
- Validating input, such as email addresses and phone numbers.
- Searching for specific patterns in text.
- Replacing parts of a string.
Key Point: Regular expressions are powerful tools for text processing and validation.
3
PHP Regular Expression Functions
PHP provides built-in functions for working with regular expressions:
preg_match(): Matches a pattern against a string.preg_match_all(): Matches a pattern against a string and returns all matches.preg_replace(): Replaces a pattern with a replacement string.
Tip: Use regular expressions for tasks that require complex string analysis.
4
Using preg_match()
The preg_match() function checks if a string matches a pattern. Example:
Key Point: Regular expressions in PHP are enclosed in delimiters, typically /.
<?php
$pattern = "/^hello/i"; // Matches "hello" at the start of the string, case-insensitive
$string = "Hello, world!";
if (preg_match($pattern, $string)) {
echo "The string matches the pattern.";
} else {
echo "No match found.";
}
?>
5
Using preg_match_all()
The preg_match_all() function finds all matches of a pattern in a string. Example:
Tip: Use preg_match_all() when you need all occurrences of a pattern in a string.
<?php
$pattern = "/[0-9]+/"; // Matches one or more digits
$string = "There are 3 apples and 5 oranges.";
preg_match_all($pattern, $string, $matches);
print_r($matches[0]); // Outputs: Array ( [0] => 3 [1] => 5 )
?>
6
Using preg_replace()
The preg_replace() function replaces matched patterns with a replacement string. Example:
Key Point: Use preg_replace() for dynamic text replacements based on patterns.
<?php
$pattern = "/cat/";
$replacement = "dog";
$string = "The cat is on the mat.";
$newString = preg_replace($pattern, $replacement, $string);
echo $newString; // Outputs: The dog is on the mat.
?>
7
Common Regular Expression Patterns
Here are some commonly used patterns:
^: Matches the start of a string.$: Matches the end of a string.[abc]: Matches any character in the brackets.[a-z]: Matches any lowercase letter.\d: Matches any digit (0-9).+: Matches one or more occurrences of the previous character.
Tip: Experiment with these patterns to get comfortable using regex.
8
Validating User Input
Regular expressions are often used to validate input. Example:
Key Point: Use regex to enforce strict input validation rules.
<?php
$email = "example@mail.com";
$pattern = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/";
if (preg_match($pattern, $email)) {
echo "Valid email address.";
} else {
echo "Invalid email address.";
}
?>
9
10
Great Job!
Congratulations! You’ve learned how to use regular expressions in PHP, including matching patterns, extracting matches, and replacing text. These skills are essential for input validation and text processing. In the next lesson, we’ll explore advanced PHP concepts like working with APIs. Let’s keep coding!

Frequently asked questions
Is the “Using Regular Expressions” lesson free?
Yes — the full text of “Using Regular Expressions” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Using Regular Expressions”?
Match and manipulate strings with regular expressions. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Using Regular Expressions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Handling Sessions and Cookies
- Working with Files
- Error and Exception Handling
- Using Regular Expressions