WikiGalaxy

Personalize

PHP Regular Expressions

Introduction to PHP Regular Expressions:

Regular expressions in PHP allow you to search and manipulate strings based on specific patterns. They are powerful tools for validating input, searching for patterns, and replacing substrings within text.

Basic Syntax:

PHP regular expressions use the PCRE (Perl Compatible Regular Expressions) syntax. Patterns are enclosed within delimiters, often slashes (/pattern/).

Common Functions:

The most common functions for regular expressions in PHP are preg_match(), preg_match_all(), preg_replace(), and preg_split().


<?php
$pattern = "/abc/";
$string = "abcdef";
if (preg_match($pattern, $string)) {
  echo "Pattern found!";
}
?>
    

Example 1: Basic Pattern Matching

This example demonstrates basic pattern matching using preg_match(). It checks if the string "abcdef" contains the pattern "abc".

Console Output:

Pattern found!

Advanced Pattern Matching

Using Character Classes:

Character classes allow you to match any one of a set of characters. For example, [aeiou] matches any vowel.

Quantifiers:

Quantifiers specify how many instances of a character or group should be matched. For instance, * matches zero or more times, + matches one or more times.


<?php
$pattern = "/[aeiou]+/";
$string = "hello world";
preg_match_all($pattern, $string, $matches);
print_r($matches);
?>
    

Example 2: Finding Vowels

This example uses preg_match_all() to find all sequences of vowels in the string "hello world".

Console Output:

Array ( [0] => Array ( [0] => e [1] => o [2] => o ) )

Pattern Replacement

Replacing Patterns:

You can replace occurrences of a pattern within a string using preg_replace(). This is useful for cleaning up input or formatting text.


<?php
$pattern = "/world/";
$replacement = "PHP";
$string = "hello world";
$new_string = preg_replace($pattern, $replacement, $string);
echo $new_string;
?>
    

Example 3: Replacing Substrings

This example uses preg_replace() to replace the word "world" with "PHP" in the string "hello world".

Console Output:

hello PHP

Pattern Modifiers

Case Insensitivity:

You can make your pattern case-insensitive by adding the i modifier. This allows you to match patterns regardless of case.


<?php
$pattern = "/HELLO/i";
$string = "hello world";
if (preg_match($pattern, $string)) {
  echo "Pattern found!";
}
?>
    

Example 4: Case-Insensitive Matching

This example demonstrates case-insensitive pattern matching. It finds "hello" in "hello world" regardless of case.

Console Output:

Pattern found!

Anchors in Regular Expressions

Using Anchors:

Anchors allow you to specify the position in the string where a match must occur. ^ matches the start of a string, while $ matches the end.


<?php
$pattern = "/^hello/";
$string = "hello world";
if (preg_match($pattern, $string)) {
  echo "String starts with 'hello'.";
}
?>
    

Example 5: Start of String Matching

This example checks if the string "hello world" starts with "hello" using the ^ anchor.

Console Output:

String starts with 'hello'.

Grouping and Capturing

Using Parentheses:

Parentheses ( ) are used for grouping and capturing parts of the pattern. This can be useful for extracting specific parts of a string.


<?php
$pattern = "/(hello) (world)/";
$string = "hello world";
preg_match($pattern, $string, $matches);
print_r($matches);
?>
    

Example 6: Capturing Groups

This example demonstrates capturing groups, extracting "hello" and "world" from the string "hello world".

Console Output:

Array ( [0] => hello world [1] => hello [2] => world )

Lookahead and Lookbehind

Using Lookaheads:

Lookaheads (?=...) and lookbehinds (?<=...) are zero-width assertions that match a group before or after a main pattern without including it in the result.


<?php
$pattern = "/hello(?= world)/";
$string = "hello world";
if (preg_match($pattern, $string)) {
  echo "Pattern found with lookahead.";
}
?>
    

Example 7: Lookahead Assertion

This example demonstrates a positive lookahead, matching "hello" only if it is followed by "world".

Console Output:

Pattern found with lookahead.

Splitting Strings with Regular Expressions

Using preg_split():

The preg_split() function splits a string by a regular expression, returning an array of substrings.


<?php
$pattern = "/[\s,]+/";
$string = "hello, world this is PHP";
$parts = preg_split($pattern, $string);
print_r($parts);
?>
    

Example 8: Splitting a String

This example uses preg_split() to split the string "hello, world this is PHP" into an array of words.

Console Output:

Array ( [0] => hello [1] => world [2] => this [3] => is [4] => PHP )

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025