In this comprehensive guide, I’ll walk you through everything you need to know about PHP Regular Expressions—from basic concepts to practical implementations that will revolutionize how you handle text data. If you are new to this domain, I highly recommend reading about regular expression basics first to internalize the concepts in a programming language-agnostic way.
Assuming you are familiar with it, let’s dive into PHP-specific bits.
PHP implements regular expressions through the PCRE (Perl Compatible Regular Expressions) library, which offers powerful pattern-matching capabilities. All PHP regex functions use the preg_
prefix, making them easy to identify.
The syntax typically follows this pattern:
preg_function('/pattern/modifiers', $subject, [other_parameters]);
Code language: PHP (php)
Let’s break down the key components:
/
that wrap your pattern (can also be other characters)PHP provides several powerful functions for working with regular expressions. Let’s dive into the most common ones with practical examples:
This function searches for a pattern in a string and returns 1 if found, 0 if not found.
// Check if a string contains a specific word
if (preg_match("/php/i", "I love PHP programming")) {
echo "Match found!"; // This will execute
} else {
echo "No match found.";
}
Code language: PHP (php)
You can also capture matched patterns:
// Extract a date from text
$text = "The event happens on 2025-05-15 at the main hall.";
if (preg_match('/(\d{4}-\d{2}-\d{2})/', $text, $matches)) {
echo "Date found: " . $matches[0]; // Outputs: Date found: 2025-05-15
}
Code language: PHP (php)
The third parameter $matches
stores all matched patterns, with the complete match at index 0 and captured groups in subsequent indices.
Unlike preg_match()
which stops at the first match, preg_match_all()
finds all occurrences of a pattern and returns the count.
$text = "Contact us at support@example.com or sales@example.com";
$count = preg_match_all('/[\w.]+@[\w.]+\.\w+/', $text, $matches);
echo "Found $count email addresses: ";
print_r($matches[0]);
// Output: Found 2 email addresses: Array ( [0] => support@example.com [1] => sales@example.com )
Code language: PHP (php)
This is extremely useful when you need to extract multiple items like all emails, phone numbers, or hashtags from a text.
This function replaces occurrences of a pattern with a specified replacement.
// Censor sensitive information
$text = "My credit card number is 1234-5678-9012-3456 and my SSN is 123-45-6789.";
$censored = preg_replace('/\d{4}-\d{4}-\d{4}-\d{4}/', 'XXXX-XXXX-XXXX-XXXX', $text);
$censored = preg_replace('/\d{3}-\d{2}-\d{4}/', 'XXX-XX-XXXX', $censored);
echo $censored;
// Output: My credit card number is XXXX-XXXX-XXXX-XXXX and my SSN is XXX-XX-XXXX.
Code language: PHP (php)
You can also use backreferences to rearrange matched content:
// Convert date format from YYYY-MM-DD to MM/DD/YYYY
$date = "2025-05-15";
$newFormat = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '$2/$3/$1', $date);
echo $newFormat; // Outputs: 05/15/2025
Code language: PHP (php)
This function divides a string into substrings based on a pattern.
// Split text by multiple delimiters
$text = "apple,banana;orange|grape:mango";
$fruits = preg_split('/[,;|:]/', $text);
print_r($fruits);
// Output: Array ( [0] => apple [1] => banana [2] => orange [3] => grape [4] => mango )
Code language: PHP (php)
This is much more flexible than PHP’s standard explode()
function which only accepts a single delimiter.
This function filters an array and returns only elements that match a pattern.
// Filter an array to find items containing numbers
$items = ["product1", "service", "item42", "category", "user123"];
$numberedItems = preg_grep('/\d+/', $items);
print_r($numberedItems);
// Output: Array ( [0] => product1 [2] => item42 [4] => user123 )
Code language: PHP (php)
Perfect for quickly filtering arrays based on complex patterns!
This function escapes special regex characters in a string, making it safe to use in a pattern.
$search = "How much is $50?"; // The $ has special meaning in regex
$quoted = preg_quote($search);
echo $quoted; // Outputs: How much is \$50\?
Code language: PHP (php)
Using preg_quote()
prevents unexpected behavior when user input contains regex metacharacters.
Here are some regex patterns you’ll find incredibly useful in your PHP projects:
$pattern = '/^[\w.%+-]+@[\w.-]+\.[a-zA-Z]{2,}$/';
$email = "user@example.com";
if (preg_match($pattern, $email)) {
echo "Valid email format";
}
Code language: PHP (php)
$pattern = '/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/';
$url = "https://www.example.com/path/to/page";
if (preg_match($pattern, $url)) {
echo "Valid URL format";
}
Code language: PHP (php)
// Require at least 8 characters, one uppercase, one lowercase, one number, and one special character
$pattern = '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/';
$password = "Secur3P@ssword";
if (preg_match($pattern, $password)) {
echo "Password meets strength requirements";
}
Code language: PHP (php)
// Format any 10-digit number to (XXX) XXX-XXXX
$phone = "1234567890";
$formatted = preg_replace('/(\d{3})(\d{3})(\d{4})/', '($1) $2-$3', $phone);
echo $formatted; // Outputs: (123) 456-7890
Code language: PHP (php)
Once you’re comfortable with the basics, these advanced techniques will take your regex skills to the next level:
Instead of accessing matches by numeric index, you can name your capture groups:
$text = "John Smith was born on 1990-05-15";
preg_match('/(?P<name>\w+ \w+) was born on (?P<date>\d{4}-\d{2}-\d{2})/', $text, $matches);
echo "Name: {$matches['name']}, Birthday: {$matches['date']}";
// Output: Name: John Smith, Birthday: 1990-05-15
Code language: PHP (php)
Sometimes you need grouping for alternation but don’t want to capture the result:
// (?:) creates a non-capturing group
preg_match('/(?:Mr|Mrs|Ms)\. (\w+)/', "Mr. Johnson", $matches);
echo $matches[1]; // Outputs: Johnson
Code language: PHP (php)
These allow you to match patterns only if they’re followed by or preceded by another pattern:
// Positive lookahead - match "price" only if followed by numbers
$text = "The price 50 dollars is reasonable, but that special price is too high.";
preg_match_all('/price(?= \d+)/', $text, $matches);
print_r($matches[0]); // Only matches the first "price"
// Negative lookbehind - match numbers not preceded by "$"
$text = "$100 and 200 euros";
preg_match_all('/(?<!\$)\d+/', $text, $matches);
print_r($matches[0]); // Only matches "200"
Code language: PHP (php)
Regular expressions are powerful but can be resource-intensive if not used correctly. Here are some tips to optimize performance:
^
and $
limit where the engine needs to search// Faster alternative to preg_match for simple substring check
$hasPhp = strpos("I love PHP", "PHP") !== false;
Code language: PHP (php)
Even experienced developers make these regex mistakes:
preg_quote()
for user input// Better than regex for email validation
if (filter_var("user@example.com", FILTER_VALIDATE_EMAIL)) {
echo "Valid email";
}
Code language: PHP (php)
Let’s see how regular expressions solve common programming challenges:
$html = '<div class="user">John <span class="age">35</span></div>';
preg_match('/<div class="user">(.+?)<span class="age">(\d+)<\/span><\/div>/', $html, $matches);
echo "User: {$matches[1]}, Age: {$matches[2]}";
// Output: User: John , Age: 35
Code language: HTML, XML (xml)
For more complex HTML parsing, consider using a dedicated parser like DOMDocument.
// Remove all HTML tags and extra whitespace
$input = "<script>alert('XSS attack');</script> Too many spaces";
$clean = preg_replace('/<.*?>/', '', $input); <em>// Remove HTML tags</em>
$clean = preg_replace('/\s{2,}/', ' ', $clean); <em>// Normalize spaces</em>
echo trim($clean); // Output: Too many spaces
Code language: PHP (php)
$csvLine = '"John Doe","john@example.com","New York, NY"';
preg_match_all('/"([^"]*)"/', $csvLine, $matches);
print_r($matches[1]);
// Output: Array ( [0] => John Doe [1] => john@example.com [2] => New York, NY )
Code language: PHP (php)
Regular expressions in PHP provide extraordinary power for text processing, validation, and manipulation. While the syntax might seem intimidating at first, the time invested in learning regex pays off tremendously in your development career.
I’ve found that regex has saved me countless hours of coding complex string operations. Start with simple patterns and gradually incorporate more advanced techniques as you become comfortable. Before long, you’ll be wielding regex like a pro!
For further learning, I highly recommend exploring the official PHP documentation on PCRE functions and practicing with regex tools like regex101.com that provide real-time testing and explanation of your patterns.
Have questions about using regular expressions in PHP? Drop a comment below. Happy coding!
Unlock the full potential of service workers with advanced features like push notifications, background sync, and performance optimization techniques that transform your web app into…
Learn how to integrate service workers in React, Next.js, Vue, and Angular with practical code examples and production-ready implementations for modern web applications.
Master the essential service worker caching strategies that transform web performance. Learn Cache-First, Network-First, and Stale-While-Revalidate patterns with practical examples that'll make your apps blazingly…
This website uses cookies.