๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_regex.asp
PHP Regular Expressions
Superglobals $GLOBALS $_SERVER $_REQUEST $_POST $_GET PHP RegEx PHP RegEx Functions
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.preg-match.php
PHP: preg_match - Manual
(?P<type>\w+)($|\((?P<length>(... ?> will output something like this: Array ( [0] => varchar(255) [type] => varchar [1] => varchar [2] => (255) [length] => 255 [3] => 255 [4] => 255 )...
๐ŸŒ
Phpliveregex
phpliveregex.com
PHP Live Regex
Test PHP regular expressions live in your browser and generate sample code for preg_match, preg_match_all, preg_replace, preg_grep, and preg_split!
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-regular-expressions
PHP | Regular Expressions - GeeksforGeeks
July 12, 2025 - POSIX Regular Expressions: Some regular expressions in PHP are like arithmetic expressions which are called POSIX regular expressions. Some times, complex expression are created by combining various elements or operators in regular expressions. The very basic regex is the one which matches a single character.
๐ŸŒ
Rexegg
rexegg.com โ€บ regex-php.php
PHP Regex Tutorial
Hit Ctrl + F to search for "preg_regex_to_pattern", "preg_pattern_error" and "preg_regex_error". If your version of PHP is 5.2.4 or later (phpinfo is your friend), you can use a wonderful PCRE escape sequence: \K. In the middle of a pattern, \K says "reset the beginning of the reported match to this point". Anything that was matched before the \K goes unreported, a bit like in a lookbehind. For example, on the string "Marlon Brando", the pattern (?i)marlon \Kbrando will return "Brando".
๐ŸŒ
PHP.Watch
php.watch โ€บ articles โ€บ php-regex-readability
Writing better Regular Expressions in PHP โ€ข PHP.Watch
May 26, 2021 - For example, the dash character (-) denominates a character range if it used between two characters, but it carries no special functionality if it used elsewhere. In the regular expression /[A-Z]/, the dash character - is used to create a range ...
๐ŸŒ
GitHub
github.com โ€บ lucleroy โ€บ php-regex
GitHub - lucleroy/php-regex: PHP Regular Expressions Builder ยท GitHub
Use Regex::matchRecursive to match recursively the whole pattern. This example matches balanced parentheses:
Author: lucleroy
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ php-tutorial โ€บ php-regular-expressions.php
Regular Expressions in PHP - Tutorial Republic
<?php $pattern = "/^color/im"; $text = "Color red is more visible than \ncolor blue in daylight."; $matches = preg_match_all($pattern, $text, $array); echo $matches . " matches were found."; ?> A word boundary character ( \b) helps you search for the words that begins and/or ends with a pattern. For example, the regexp /\bcar/ matches the words beginning with the pattern car, and would match cart, carrot, or cartoon, but would not match oscar.
Find elsewhere
๐ŸŒ
Regexbuddy
regexbuddy.com โ€บ php.html
PHP ereg and preg Regex Functions
Since PHP 7.3.0 they are based on PCRE2. The ereg functions implement POSIX regular expressions which offer a limited syntax. The ereg functions are deprecated, but RegexBuddy still fully supports them so you can maintain existing code or convert your regular expressions from PHPโ€™s ereg flavor ...
๐ŸŒ
Jotform
jotform.com โ€บ blog โ€บ advice โ€บ getting started with php regular expressions
Getting Started with PHP Regular Expressions | Jotform Blog
June 25, 2024 - As an addition in PHP the forward slash character is escaped using the simple slash \. Example: โ€˜/he\/llo/โ€™ ยท To have a brief understanding how these operators are used, letโ€™s have a look at a few examples: Besides operators, there are regular expression modifiers, which can globally alter the behavior of search patterns. The regex modifiers are placed after the pattern, like this โ€˜/hello/iโ€™ and they consists of single letters such as i which marks a pattern case insensitive or x which ignores white-space characters.
๐ŸŒ
RegexOne
regexone.com โ€บ references โ€บ php
RegexOne - Learn Regular Expressions - PHP
$1 and $2). $regex = "/([a-zA-Z]+) (\d+)/"; $new_string = preg_replace($regex, "$2 of $1", "June 24"); // The string returned is either the same input string if the // regex does not match, or the transformed string. // This prints "24 of June" echo $new_string; For more information about using ...
๐ŸŒ
Webmaster World
webmasterworld.com โ€บ php โ€บ 4821.htm
Regex Patterns & Extracting URLs - PHP Server Side Scripting forum at WebmasterWorld - WebmasterWorld
What's more, I'd prefer if I could match the second URL with only the [thumbs.example.com...] part so that it doesn't matter about the other slashes. As I mentioned earlier, I'm also trying to extract the URLs in a bunch of HTML. I've managed to get a regex pattern to work for links [using preg_match_all() to get the data] but I also seem to be saving the rest of the link tag to the array, which I don't want to do.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ php-tutorial โ€บ php regular expressions
PHP Regular Expressions - Scaler Topics
April 1, 2024 - Here are some of the most commonly ... a dot (.) and is used to combine two or more patterns. For example, the regular expression "cat" ....
๐ŸŒ
Envato Tuts+
code.tutsplus.com โ€บ home โ€บ coding fundamentals โ€บ regular expressions
How to Work With Regular Expressions in PHP | Envato Tuts+
April 20, 2021 - Letโ€™s have a look at the following example to understand how it works. Here you can see we use a pattern that matches line breaks, and the output returns the individual lines of the text. Today, we discussed regular expression functions in PHP.
๐ŸŒ
Medium
mohasin-dev.medium.com โ€บ regex-regular-expression-in-php-you-should-know-f14ee1606b90
Regex(Regular Expression) in PHP You Should Know | by Mohasin Hossain | Medium
July 16, 2022 - Most common regex syntax or pattern and their meaning or description ... PHP regular expressions are strings with pattern enclosing in delimiters for example "/pattern/".
Top answer
1 of 1
2

Method

  1. You need to strip out the variable patterns; you can use preg_match_all to do this

    preg_match_all("/(\[\w+\]|\([\w|]+\))/", '[ct]hun(k|der)(s|ed|ing)?', $matches);
    
    /* Regex:
    
    /(\[\w+\]|\([\w|]+\))/
    /                       : Pattern delimiter
     (                      : Start of capture group
      \[\w+\]               : Character class pattern
             |              : OR operator
              \([\w|]+\)    : Capture group pattern
                        )   : End of capture group
                         /  : Pattern delimiter
    
    */
    
  2. You can then expand the capture groups to letters or words (depending on type)

    $array = str_split($cleanString, 1); // For a character class
    $array = explode("|", $cleanString); // For a capture group
    
  3. Recursively work your way through each $array

Code

function printMatches($pattern, $array, $matchPattern)
{
    $currentArray = array_shift($array);

    foreach ($currentArray as $option) {
        $patternModified = preg_replace($matchPattern, $option, $pattern, 1);
        if (!count($array)) {
            echo $patternModified, PHP_EOL;
        } else {
            printMatches($patternModified, $array, $matchPattern);
        }
    }
}

function prepOptions($matches)
{
    foreach ($matches as $match) {
        $cleanString = preg_replace("/[\[\]\(\)\?]/", "", $match);
        
        if ($match[0] === "[") {
            $array = str_split($cleanString, 1);
        } elseif ($match[0] === "(") {
            $array = explode("|", $cleanString);
        }
        if ($match[-1] === "?") {
            $array[] = "";
        }
        $possibilites[] = $array;
    }
    return $possibilites;
}

$regex        = '[ct]hun(k|der)(s|ed|ing)?';
$matchPattern = "/(\[\w+\]|\([\w|]+\))\??/";

preg_match_all($matchPattern, $regex, $matches);

printMatches(
    $regex,
    prepOptions($matches[0]),
    $matchPattern
);

Additional functionality

Expanding nested groups

In use you would put this before the "preg_match_all".

$regex        = 'This happen(s|ed) to (be(come)?|hav(e|ing)) test case 1?';

echo preg_replace_callback("/(\(|\|)(\w+)(?:\(([\w\|]+)\)\??)/", function($array){
    $output = explode("|", $array[3]);
    if ($array[0][-1] === "?") {
        $output[] = "";
    }
    foreach ($output as &$option) {
        $option = $array[2] . $option;
    }
    return $array[1] . implode("|", $output);
}, $regex), PHP_EOL;

Output:

This happen(s|ed) to (become|be|have|having) test case 1?

Matching single letters

The bones of this would be to update the regex:

$matchPattern = "/(?:(\[\w+\]|\([\w|]+\))\??|(\w\?))/";

and add an else to the prepOptions function:

} else {
    $array = [$cleanString];
}

Full working example

function printMatches($pattern, $array, $matchPattern)
{
    $currentArray = array_shift($array);

    foreach ($currentArray as $option) {
        $patternModified = preg_replace($matchPattern, $option, $pattern, 1);
        if (!count($array)) {
            echo $patternModified, PHP_EOL;
        } else {
            printMatches($patternModified, $array, $matchPattern);
        }
    }
}

function prepOptions($matches)
{
    foreach ($matches as $match) {
        $cleanString = preg_replace("/[\[\]\(\)\?]/", "", $match);
        
        if ($match[0] === "[") {
            $array = str_split($cleanString, 1);
        } elseif ($match[0] === "(") {
            $array = explode("|", $cleanString);
        } else {
            $array = [$cleanString];
        }
        if ($match[-1] === "?") {
            $array[] = "";
        }
        $possibilites[] = $array;
    }
    return $possibilites;
}

$regex        = 'This happen(s|ed) to (be(come)?|hav(e|ing)) test case 1?';
$matchPattern = "/(?:(\[\w+\]|\([\w|]+\))\??|(\w\?))/";

$regex = preg_replace_callback("/(\(|\|)(\w+)(?:\(([\w\|]+)\)\??)/", function($array){
    $output = explode("|", $array[3]);
    if ($array[0][-1] === "?") {
        $output[] = "";
    }
    foreach ($output as &$option) {
        $option = $array[2] . $option;
    }
    return $array[1] . implode("|", $output);
}, $regex);


preg_match_all($matchPattern, $regex, $matches);

printMatches(
    $regex,
    prepOptions($matches[0]),
    $matchPattern
);

Output:

This happens to become test case 1
This happens to become test case 
This happens to be test case 1
This happens to be test case 
This happens to have test case 1
This happens to have test case 
This happens to having test case 1
This happens to having test case 
This happened to become test case 1
This happened to become test case 
This happened to be test case 1
This happened to be test case 
This happened to have test case 1
This happened to have test case 
This happened to having test case 1
This happened to having test case 
๐ŸŒ
CatsWhoCode
catswhocode.com โ€บ development & programming โ€บ php regex for web developers
PHP Regex for Web Developers
January 9, 2025 - Regular expressions are a very useful tool for developers. They allow to find, identify or replace a word, character or any kind of string. This tutorial will teach you how to master PHP regexp and show you extremely useful, ready-to-use PHP regular expressions that any web developer should ...
๐ŸŒ
Medium
medium.com โ€บ @olivia.j.01101001 โ€บ how-to-use-php-regular-expressions-for-pattern-matching-and-data-validation-d58dacb06ea1
How to Use PHP Regular Expressions for Pattern Matching and Data Validation | by Olivia J | Medium
June 11, 2023 - To use the preg_ functions, you need to enclose your regex in delimiters (such as /, #, or ~) and optionally add modifiers (such as i, m, or g) after the closing delimiter. For example: