Preg Functions

Regex

Use Regex 101 to build and test patterns, you will see a break down of the pattern and the results from the test string.

A helpful cheat sheet (want to find better):

https://courses.cs.washington.edu/courses/cse154/14au/cheat-sheets/php-regex-cheat-sheet.pdf

Functions

preg_filter

preg_filter() is identical to preg_replace() except it only returns the (possibly transformed) subjects where there was a match.

3v4l Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php

$string = "The Chernobyl disaster was a catastrophic nuclear accident that occurred on 26 April 1986, in the No. 4 nuclear reactor at the Chernobyl Nuclear Power Plant, near the city of Pripyat, in the north of the Ukrainian SSR.";

$pattern = '%[A-Z]\w+%';
$replacement = "****";

$newString = preg_filter($pattern, $replacement, $string);

print($newString);

preg_grep

3v4l

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php

/* Finding Floats in an Array */

$arr = [
    // Short String
    '123',
    // Long String
    '123 and another float at the end 123.23',
    // Mixed String
    '4.7 + 99 / 8...8 00.b',
    // Float
    123.444,
    // Decimal
    444,
    ];

$pattern = '%(\d+)?\.\d+%';

var_dump(preg_grep($pattern, $arr));

/* Find occurrences of either a space at the start of the string or a "ing" word */

$arr2 = [
    "Examine she brother prudent add day ham.\n
    Far stairs now coming bed oppose hunted become his. You zealously departure had procuring suspicion. Books whose front would purse if be do\n decay. Quitting you way formerly disposed perceive ladyship are. Common turned boy direct and yet.",
    "Behind sooner dining so window excuse he summer.       \n Breakfast met certainty and fulfilled propriety led. Waited get either are wooded little her. Contrasted unreserved as mr particular\n collecting it everything as indulgence. Seems ask meant merry could put. Age old begin had boy noisy table       front whole given.",
    "Enjoying minutes related as at on on. Is dried as often me. Goodness as reserved raptures to mistaken steepest oh screened he. Gravity he mr sixteen esteems. Mile home its new way with high told said. Finished no horrible blessing landlord\n dwelling dissuade if.       Rent fond am he in on read. Anxious cordial demands settled entered in do to colonel.",
    " <p>Remaining lively hardly needed at do by. Two you fat downs three.\n True mr gone most at. Dare as name just when with it body. Travelling inquietude      she increasing off impossible the. Cottage be noisier looking to we promise on. Disposal to kindness appetite diverted learning of on raptures.<br> Betrayed any may returned now dashwood formerly. Balls way delay shy man views. No so instrument discretion unsatiable to in.</p>",
    ];

$pattern = '%^(\s+)|^(\w+)(ing)%';

var_dump(preg_grep($pattern, $arr2));

/* Invert results with Flag */
var_dump(preg_grep($pattern, $arr2, PREG_GREP_INVERT));

preg_last_error - Dale

This returns the error code of the last PCRE regex which has been executed.

Return constants (from php.net):

1
2
3
4
5
6
7
PREG_NO_ERROR
PREG_INTERNAL_ERROR
PREG_BACKTRACK_LIMIT_ERROR
PREG_RECURSION_LIMIT_ERROR
PREG_BAD_UTF8_ERROR
PREG_BAD_UTF8_OFFSET_ERROR
PREG_JIT_STACKLIMIT_ERROR

preg_match_all

Can be used to capture groups.

3v4l Example

1
2
3
4
5
6
7
8
9
<?php

$string = "The Chernobyl disaster was a catastrophic nuclear accident that occurred on 26 April 1986, in the No. 4 nuclear reactor at the Chernobyl Nuclear Power Plant, near the city of Pripyat, in the north of the Ukrainian SSR.";

$pattern = '%[A-Z]\w+%';

preg_match_all($pattern, $string, $matches);

print_r($matches);

preg_match

3v4l Link

Printf-Test

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Mauris commodo quis imperdiet massa. Turpis egestas pretium aenean pharetra. In hac habitasse platea dictumst ves33tibulum rhoncus est pellentesque. Purus faucibus ornare suspendisse sed nisi lacus. Malesuada bibendum arcu vitae elementum curabitur vitae nunc sed velit. Aliquam ultrices sagittis orci a scelerisque. Lorem123 ipsum dolor sit amet consectetur adipiscing. Tristique senectus et netus et malesuada fames ac. Eget duis at tellus at urna. Auctor eu augue ut lectus arcu bibendum at. In ante metus dictum at tempor com444modo ullamcorper a. Dolor magna eget est lorem ipsum. Lorem dolor sed viverra ipsum nunc aliqu666et bibendum. Urna duis convallis convallis tellus id.";


$pattern = 
    # Word before a comma
    # -- Positive look ahead assertion
    '%\w+(?=,)'
    # All Characters inbetween up to the next comma
    # -- Negative look after assertion
    .'.*(?<=,)%';
preg_match($pattern, $string, $match);
var_dump($match);


$pattern = 
    # Get all words that contain the letter p and are just before a full stop.
    '%(\w+p\w+)(?=\.)%';
;
# Match will contain a subgroup match, notice the extra returned value in array.
preg_match($pattern, $string, $match);
var_dump($match);

#### Flags

# PREG_OFFSET_CAPTURE
# -- Return the offset from each result given in match - integer for location in string
preg_match($pattern, $string, $match, PREG_OFFSET_CAPTURE);
var_dump($match);

# Offset in string
preg_match($pattern, $string, $match, PREG_OFFSET_CAPTURE, 250);
var_dump($match);

$pattern =
# Find the first mixed number and letter word.
# Remove unfound subpatterns with null with optional flag
'%\w+\d+(999)*\w+%';
#  PREG_UNMATCHED_AS_NULL
preg_match($pattern, $string, $match, PREG_UNMATCHED_AS_NULL);
var_dump($match);

preg_quote

preg_replace_callback_array

3v4l Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<?php

$string = "The Chernobyl disaster was a catastrophic nuclear accident that occurred on 26 April 1986, in the No. 4 nuclear reactor at the Chernobyl Nuclear Power Plant, near the city of Pripyat, in the north of the Ukrainian SSR.";

$pattern1 = '%[A-Z]\w+%';
$pattern2 = '%\d+%';

preg_replace_callback_array(
    [
        $pattern1 => function ($match) {
            echo "pattern 1: \n";
            print_r($match);
        },
        $pattern2 => function ($match) {
            echo "pattern 2: \n";
            print_r($match);
        }
    ],
    $string
);

preg_replace_callback

3v4l Link

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

$string_original = "
Ivq6T2IoWbLNCoru7RTv
kcEDwTVDhkqRAePOOQsM
c6pcDsaq1waWzuzNHAYl
pqFRFt1XwSX6JBLslwDs
VWygeUir8kWMOpU5RArI
WZ1UCYybMhpetyaCCkiM
8OVqENVpWmCZvDyvq8dG
PPxPs48xPuiaLGdKhjEl
2i5y2URwQlRS1pbrKDS5
euT3KHvPrhM5wnOyTpJv
";
var_dump($string_original);

$string = $string_original;

$pattern = '%'
# Select a digit and 5 letters after
.'(\d)(\w{5})'
.'%';

$string = preg_replace_callback(
    $pattern,
    function($matches){
        # Add to one to digit and then lowercase 5 letters after.
        $matches[1] = $matches[1] + 1;
        $matches[2] = strtolower($matches[2]);
        return $matches[1].$matches[2];
    }, 
    $string);
var_dump($string);

$string = $string_original;
$pattern = '%'
# Any digit and after 5 alpha-numeric characters.
.'(\d)(?<=\w{5})'
.'%';

# Replace all integers to symbols
$string = preg_replace_callback(
    $pattern,
    function($matches){
        if($matches[1] >= 5){
            return '_';
        }else{
            return '@';
        }
    }, 
    $string,
    # Limit only 2 replacements
    2);
var_dump($string);

# Array Replacement
$arr = [
    'IUjd5x13NF1OyQn9Afjy',
'S1BG0EhuWxr5icrgD1St',
'oLKV7cI6Qx5503K0eJ6U',
'V36IgE0TE6Qq6beBnJkC',
'jcMIKR9wk9u8kdN6UyPR',
'N8CNqZDQPJwLH87rnmKq',
'yeSAo75DcWQqQM9FynIu',
'QoRWAnP2G1LGkRoBbiud',
'tiVkR9UkIiB6ChI6l6Se',
'9BWLYV7msRz7WsSADx7u'
    ];

$pattern = '%'
# Any Digit after 10 characters
.'(\d)(?<=\w{10})'
.'%';

# Replace integers from <5 to _ and >5 to @
# Note the limit here works per line in the array.
# And the count can be gathered via another variable declaration on the function.
$arr = preg_replace_callback(
    $pattern,
    function($matches){
        if($matches[1] >= 5){
            return '_';
        }else{
            return '@';
        }
    }, 
    $arr,
    1,
    $count);
var_dump($arr);
var_dump($count);

preg_replace

Replace all instances of a matched regular expression.

3v4l

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?php

$subject = <<<'STR'
$array = ['bananna', 'cranberry', 'apple'];
$fn = function ($a, $b) {
    if(strlen($a) > strlen($b)) {
        return 1;
    }
    return 0;
};
usort($array, $fn);
STR;

$pattern = '%\'\w*\'%';
$replacement = 'Chocolate';

$newStory = preg_replace($pattern, $replacement, $subject);

print($newStory);

preg_split

Splits a string into an array of strings based on a regex.

3v4l

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php

$subject = <<<STR
Turicum was a Swiss automobile manufactured between 1904 and 1906 in Zurich, 1907 till 1912 in Uster. Turicum is the Latin name of Zurich. The first car made by Martin Fischer was a small single-seater go-kart like vehicle, 140 cm long, with pedal-operated steering and powered by a motorcycle engine. It had chain drive but a second prototype had friction drive. The friction drive consisted of a flat steel disc coupled to the engine which drove a small, leather-covered wheel running at right angles to its surface. The small wheel could be moved across the surface of the large disc, giving an infinitely variable gear ratio. They attracted little public interest. The Swiss industrialist Jakob Heusser was also involved to establish the car manufacturer.
STR;

$pattern = '%[.] %';

$result = preg_split($pattern, $subject);

var_dump($result);