Combine PHP Functions To Make Your Own

Combine PHP Functions To Make Your Own

One of the great things about PHP is the fact that it has built in functions for anything and everything. A really neat thing to do is to try to stitch these functions together to create interesting new functions. If there are functions in PHP that you really like and want to extend them so to speak, you can do this by creating your own. In this episode, we’ll take a look at doing just that. We’ll have a look at preg_match_all, array_count_values, and arsort, to create a new and awesome function named preg_count_sort. Let’s check it out.

The Native Functions

First off, we’ll take a quick look at the native PHP functions we’ll test out here.


This function accepts a regular expression pattern, a subject to match against, and the name of a variable which will hold an array of matches.


This function takes an array as input, then counts the number of times each value happens in the provided array. It then creates a new array, with the keys of the new array being the original values, and the values now containing a count of how many times the original value appeared in the original array.


There are a tremendous amount of array functions in PHP so you can find whatever you need for sorting. This one sorts the provided array in reverse order and keeps the original keys in tact, which we need for our little application here.

The Function of Functions

Now we’ll take a look at how to combine the three functions we mentioned here into one cool function of our own. We’re going to call it, the preg_count_sort function.


Here is the source code for the function.

Let’s talk about how it works. First off, we simply define the function by using the function keyword followed by the name of the function we wish to declare. Note that this function takes a pattern, which will be a regular expression, and a subject, which will be the data against which the regular expression will run. Next up, we open up a table tag since we are going to put the results of our function into a nice tabular output. On the next line we make use of the first of the original PHP functions, preg_match_all. This function takes the regular expression pattern as the first parameter, the subject to match against as the second, and the name of the array to hold any matches as the third. Note that we include the starting and ending delimiters for the pattern by including the forward slash at the beginning and end of the pattern. This way, when we provide the regular expression in our application, we don’t have to also provide the beginning and ending delimiters. array_count_values is the second native php function to make use of. This counts the number of times each match occurred in the matches array which the preg_match_all function populated. Once we have the count of matches, we use the third native PHP function arsort, to sort the counts from highest to lowest in number. The foreach loop simply loops through all the matches placing them into rows and cells within the overall table. Lastly, we just go ahead and close out the table tag.

Put The New Function To Use

We’ll put this code to use in a little single page application and test it out. Let’s see it now.

preg_count_sort.php source

So what does this little app do for us? Well, we can test it out. Let’s define our Subject and pattern, then run the program.

Our subject will be this text string: “If you like apple products, you might have an iPad, iPhone, or even an iMac. If the macbook pro is not your thing, you might like the Lenovo Carbon X1. The latest generation iMac is fantastic, however the decade old iMac sitting on my desk is more of a collectors item at this point. No need of a new iPad yet, the original retina display version still works just fine.”

Our pattern will be this expression: i[A-Z][a-z]*

Here is the result when we run the program
Combine PHP Functions To Make Your Own

Pretty Cool! When we ran the function, we found that in the subject iMac occured 3 times, iPad occurred twice, and iPhone occurred once. Of course this is a bit of a nonsense example, however if you brush up on your regular expressions and provide a lengthy meaningful subject, you can uncover all kinds of interesting data.


This quick episode took a look at combining PHP functions together to create your own. If you use your imagination, you can take this approach with any number of functions which accept and output data to create your own custom made solutions.