SakhaliaNetHome PageMilitary HistoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Regex (III)

PHP Example #128

Back references with preg_match()

The text captured in arrays can be part of a pattern by using back references, which are metacharacters inside a pattern that make reference to captured strings by number. They are indicated by a backslash followed by the number corresponding to the captured string. In this example back references are used to match opening and closing HTML tags. The backreferences in the two first patterns ensure that the closing tag matches the opening tag, this is, the "b" in the opening tag has to have a "/b" in the closing tag. This is why the line "Bad, Backreferences" is not printed, because the string $bad_html does not fit with the requirement of having matching tags. The patterns that do not use back references will find a match in both strings, since they only require that the tags are either "b" or "i", and therefore both lines "No Backreferences" are printed.

<?php
$ok_html = "I <b>love</b> shrimp dumplings.";
$bad_html = "I <b>love</i> shrimp dumplings.";
// Using back references
if (preg_match('@<([bi])>.*?</\\1>@', $ok_html)) {
print "Good for you! (OK, Backreferences)<br/>";
}
if (preg_match('@<([bi])>.*?</\\1>@', $bad_html)) {
print "Good for you! (Bad, Backreferences)<br/>";
}
// Not using back references
if (preg_match('@<([bi])>.*?</[bi]>@', $ok_html)) {
print "Good for you! (OK, No Backreferences)<br/>";
}
if (preg_match('@<([bi])>.*?</[bi]>@', $bad_html)) {
print "Good for you! (Bad, No Backreferences)<br/>";
}
?>
Good for you! (OK, Backreferences)
Good for you! (OK, No Backreferences)
Good for you! (Bad, No Backreferences)

PHP Example #129

Replacing with preg_replace()

The function preg_replace() searches for parts of a string that match with a pattern and then replace those parts with another text. The arguments to pass to the function are a pattern, the text which will be used to replace, and a string to search for, as shown in this example. The function returns the changed string. In this example a regular expression that matches with e-mail addresses is used to replace any e-mail address with the string "[ address removed ]".

The syntax <<<TEXT and TEXT; is what is called a heredoc, which allows to store with ease a piece of text enclosed within tags in a variable (TEXT is the name of the tags in this example). Heredocs are tricky however, and often produce parser errors, so I avoided them along these tutorials. In this case the heredoc didn't worked until I aligned the closing tag (TEXT;) to the first column of the line, and also I had to do the same with the rest of the text so it would be displayed in the correct place in the webpage. If you can't make the heredoc to work for any reason, you will have to use multiple string concatenations to get all the text stored in the variable.

<?php
$members = <<<TEXT
Name            		E-mail Address
---------------------------------------------------------------
Inky T. Ghost			inky@pacman.example.com
Donkey K. Gorilla		kong@banana.example.com
Mario A. Plumber		mario@franchise.example.org
Bentley T. Bear			bb@xtal-castles.example.net
TEXT;

print '<pre>';
print preg_replace('/[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}/',
'[ address removed ]', $members);
print '</pre>';
?>
									  
Name            		E-mail Address
---------------------------------------------------------------
Inky T. Ghost			[ address removed ]
Donkey K. Gorilla		[ address removed ]
Mario A. Plumber		[ address removed ]
Bentley T. Bear			[ address removed ]

PHP Example #130

Replacing with preg_replace()

This example is a variation of the previous one, but instead of completely removing the addresses, it uses back references to replace the symbol @ with the string "at".

<?php
$members = <<<TEXT
Name            		E-mail Address
---------------------------------------------------------------
Inky T. Ghost			inky@pacman.example.com
Donkey K. Gorilla		kong@banana.example.com
Mario A. Plumber		mario@franchise.example.org
Bentley T. Bear			bb@xtal-castles.example.net
TEXT;

print '<pre>';
print preg_replace('/([^@\s]+)@(([-a-z0-9]+\.)+[a-z]{2,})/',
'\\1 at \\2', $members);
print '</pre>';
?>
									  
Name            		E-mail Address
---------------------------------------------------------------
Inky T. Ghost			inky at pacman.example.com
Donkey K. Gorilla		kong at banana.example.com
Mario A. Plumber		mario at franchise.example.org
Bentley T. Bear			bb at xtal-castles.example.net

PHP Example #131

Processing arrays with preg_split()

The function preg_split() is a tricky version of the function explode(), in which the delimiter that divides a string is a regular expression instead of a literal sequence of characters. This example uses preg_split() in a string that contains a list of things that are delimited by different characters. A third argument for preg_split() sets a maximum number of elements in the returned list. When the number of elements is limited, preg_split() places all the additional elements in the last element.

<?php
$sea_creatures = "cucumber;jellyfish, conger eel,shrimp, crab roe; bluefish";
// Separates the string with a comma or a semicolon followed by
// an optional space
$creature_list = preg_split('/[,;] ?/', $sea_creatures);
print "Would you like some $creature_list[2]?";
print '<br>';
// Separates the string in three elements
$creature_list = preg_split('/, ?/', $sea_creatures, 3);
print "The last element is $creature_list[2]";
?>
Would you like some conger eel?
The last element is shrimp, crab roe; bluefish

PHP Example #132

Processing arrays with preg_split()

If there are two successive delimiters in the string, preg_split() inserts an empty string in the array that returns. Usually we wouldn't want preg_split() to return empty elements in the array, so we would use as a fourth argument the constant PREG_SPLIT_NO_EMPTY. When this argument is passed, we have to specify a limit as the third argument or pass -1 if there is no limit. This example uses this feature to count the number of words in $text.

<?php
$text = <<<TEXT
"It's time to ring again," said Tom rebelliously.
"I agree! I'll help you," said Jerry resoundingly.
TEXT;

// Takes each of the words in $text, but does not place whitespace and
// punctuation in $words. The -1 for the argument of limit means
// "there is no limit"
$words = preg_split('/[",.!\s]/', $text, -1, PREG_SPLIT_NO_EMPTY);
print 'There are ' . count($words) . ' words in the text.';
?>
There are 16 words in the text.

PHP Example #133

Processing arrays with preg_grep()

The function preg_grep() finds elements in an array whose values match with a regular expression. This example uses preg_grep() for finding all the words that contain double (consecutive) letters.

<?php
$text = <<<TEXT
"It's time to ring again," said Tom rebelliously.
"I agree! I'll help you," said Jerry resoundingly.
TEXT;

$words = preg_split('/[",.!\s]/', $text, -1, PREG_SPLIT_NO_EMPTY);
// Finds the words that contain double letters
$double_letter_words = preg_grep('/([a-z])\\1/i', $words);
foreach ($double_letter_words as $word) {
print "$word<br/>";
}
?>
rebelliously
agree
I'll
Jerry