X
X

Revamped regular expression function

Sometimes we work with string data to attain more information. For example there are spam filters that need to check whether a comment doesn’t contain certain ‘key phrases’ for advertising or spam. There are also lots of other reasons why you’d want to do this.

PHP offered the preg_replace_callback function among many others that allows for regular expressions or RegEx. Regular expressions are really fun to work with; in its own right it is a language in and of itself. You can play with regular expressions on one of my beloved websites regex101.com, definitely worth bookmarking.

PHP7 plus, now offers us the new preg_replace_callback_array function which is more suitable and has a cleaner syntax than the old preg_replace_callback function. This new function allows for easy callbacks to be invoked when there is a successful match. Here is the example code we’re working with…

<?php
$subject = 'Aaaaaa aa Bbb';
echo 'Subject <b>' . $subject . '</b> <br> <br>';
preg_replace_callback_array(
	[
	'~[a]+~i' => function( $match ) { echo $match[0] . ' match found for [a] <br>';  },
	'~[b]+~i' => function( $match ) { echo $match[0] . ' match found for [b] <br>'; }
	],
	$subject
);

… Here we have a $subject string to check for certain words, phrases or characters in the string…

$subject = 'Aaaaaa aa Bbb';

… Next I just want to echo this out so we can see the subject string on the page…

echo 'Subject <b>' . $subject . '</b> <br> <br>';

… Producing…

Subject Aaaaaa aa Bbb

… Then I want to check the subject string with regular expressions, using the new preg_replace_callback_array function…

preg_replace_callback_array(
	[
		'~[a]+~i' => function( $match ) { echo $match[0] . ' match found for [a] <br>';  },
		'~[b]+~i' => function( $match ) { echo $match[0] . ' match found for [b] <br>'; }
	],
	$subject
);

… The first parameter is an associative array…

[
	'~[a]+~i' => function( $match ) { echo $match[0] . ' match found for [a] <br>';  },
	'~[b]+~i' => function( $match ) { echo $match[0] . ' match found for [b] <br>'; }
]

… The keys of the associative array are the regular expressions we check for…

'~[a]+~i'
'~[b]+~i'

… The first regular expression ‘~[a]+~i’ checks for words consisting of only the [a] letter upper or lower case. Likewise the second regular expression ‘~[b]+~i’ checks for words only consisting of the letter [b]. Each regular expression key is coupled with a function, this function will be invoked on every successful match found for that expression…

[
	'~[a]+~i' => function( $match ) { echo $match[0] . ' match found for [a] <br>';  },
	'~[b]+~i' => function( $match ) { echo $match[0] . ' match found for [b] <br>'; }
]

… The second parameter is the string data we want to run our regular expression’s on. Note I already placed this string inside of my $subject variable…

preg_replace_callback_array(
	[
		'~[a]+~i' => function( $match ) { echo $match[0] . ' match found for [a] <br>';  },
		'~[b]+~i' => function( $match ) { echo $match[0] . ' match found for [b] <br>'; }
	],
	$subject
);

… Our subject string ‘Aaaaaa aa Bbb’ contain’s two words consisting only of the letter [a], ‘Aaaaaa aa Bbb’, and one word consisting of the letter [b], ‘Aaaaaa aa Bbb‘. So the first regular expression will have it’s callback function invoked twice…

'~[a]+~i' => function( $match ) { echo $match[0] . ' match found for [a] <br>';  }

… Producing…

Aaaaaa match found for [a] 
aa match found for [a]

… Notice the function fired twice because two matches where made. Also note the successfully matched portion of the string was returned to the function. That’s why in the callback function has the $match argument which is passed an array populated with the found string, function( $match ),. So for example the regular expression…

'~[a]+~i'

… This first matched ‘Aaaaaa aa Bbb’ and it passed that portion of the string to the $match argument in our callback function…

'~[a]+~i' => function( $match ) { echo $match[0] . ' match found for [a] <br>';  }

… Echoing out…

Aaaaaa match found for [a]

… Next it found the second word consisting only of the letter [a] ‘Aaaaaa aa Bbb’ which again invoked the same function passing that part of the string to the $match argument…

aa match found for [a]

… There are no more words consisting only of the letter [a]. So now we move onto the next regular expression ‘~[b]+~i’. This will find ‘Aaaaaa aa Bbb‘ portion of the subject string and run it to the relevant callback function…

'~[b]+~i' => function( $match ) { echo $match[0] . ' match found for [b] <br>';  }

… Producing…

Bbb match found for [b]

… So here’s the final output of my preg_replace_callback_array function…

Aaaaaa match found for [a] 
aa match found for [a] 
Bbb match found for [b]

That’s it, PHP has provided a really nice way to check a string for multiple regular expressions and then callback a function on each successful match of that regular expression.

Latest Posts