How search strings or replace single or multiple matches using PHP functions?
The preg_match() function can be used to match a regular expression against a given string. The function returns true if the match is successful, and can return all the captured sub-patterns in an array if an optional third parameter is passed by reference. Here’s an example:
name = “John Doe”;
// Simple match
$regex = “/[a-zA-Z\s]/”;
if (preg_match($regex, $name))
{
// Valid name }
// Match with sub-patterns and capture
$regex = ‘/^(\w+)\s(\w+)/’;
$matches = array();
if (preg_match ($regex, $name, $matches))
{
var_dump ($matches);
}
If you run the example, you will notice that the $matches array is populated, on return with the following values:
array(3) {
[0]=>string(12) “John Doe”
[1]=>string(5) “John”
[2]=>string(6) “Doe”
}
The first element of the array contains the entire matched string, while the second element (index 1) contains the first captured sub-pattern, and the third element contains the second matched sub-pattern.
Performing multiple matches:
The preg_match_all() function allows you to perform multiple matches on a given string based on a single regular expression. For example:
$string = “a1bb b2cc c2dd”;
$regex = “#([abc])\d#”;
$matches = array();
if (preg_match_all ($regex, $string, $matches))
{
var_dump ($matches);
}
This script outputs the following:
array(2) {
[0]=>
array(3) {
[0]=>
string(2) “a1″
[1]=>
string(2) “b2″
[2]=>
string(2) “c2″
}
[1]=>
array(3) {
[0]=>
string(1) “a”
[1]=>
string(1) “b”
[2]=>
string(1) “c”
}
}
All the whole-pattern matches are stored in the first sub-array of the result, while the first captured subpattern of every match is stored in the corresponding slot of the second sub-array.
Using PCRE to Replace Strings:
Whilst str_replace() is quite flexible, it still only works on ‘whole’ strings, that is, where you know the exact text to search for. Using preg_replace(), however, you can replace text that matches a pattern we specify. It is even possible to reuse captured subpatterns directly in the substitution string by prefixing their index with a dollar sign. In the example below, we use this technique to replace the entire matched pattern with a string that is composed using the first captured subpattern ($1).
$body = “[b]Make Me Bold![/b]”;
$regex = “@\[b\](.*?)\[/b\]@i”;
$replacement = ‘$1‘;
$body = preg_replace($regex, $replacement, $body);
Just like with str_replace(), we can pass arrays of search and replacement arguments; however, unlike str_replace(), we can also pass in an array of subjects on which to perform the search-and-replace operation. This can speed things up considerably, since the regular expression (or expressions) are compiled once and reused multiple times.
$subjects[’body’] = “[b]Make Me Bold![/b]”;
$subjects[’subject’] = “[i]Make Me Italics![/i]”;
$regex[] = “@\[b\](.*?)\[/b\]@i”;
$regex[] = “@\[i\](.*?)\[/i\]@i”;
$replacements[] = “$1“;
$replacements[] = “$1“;
$results = preg_replace($regex, $replacements, $subjects);
When you execute the code shown above, you will end up with an array that looks like this:
array(2) {
[”body”]=>
string(20) “Make Me Bold!”
[”subject”]=>
string(23) “Make Me Italic!”
}
Notice how the resulting array maintains the array structure of our $subjects array that we passed in, which, however, is not passed by reference, nor is it modified.
This concludes the basic of working with these functions, which are essential to any programmer. So be careful to understand them as they’re really indispensable.
