|

PHP String Helper Functions

php string helper functions

PHP is incredibly powerful right out of the box, even if you aren’t using any frameworks. We need to be fluent with the language itself as well as the frameworks we might like to use. Using the built in functions of the language we can create our own functions, or wrapper functions, of the built in ones. Sometimes this helps with just being able to more closely match your desired workflow. In this example we are going to create some String Helper functions to use that will help us to quickly work on strings in a very easy way. We’ll make use of 6 built in PHP functions, those being, strtolower, strpos, substr, strlen, preg_match_all, and str_replace, to build 4 new string helper functions. Let’s do it!

Initial Configuration

Let’s first define some constants so that we can use these values in the helper functions.

split_string()

First up is the split_string() function. It allows us to break a string into two pieces based on a setpoint, and then grab either side of the two pieces with or without the setpoint. It uses the built in PHP functions strtolower, strpos, and substr.

find_between()

Next up is the find_between() helper function. This handy guy basically just makes use of the split_string() function to help us to find a substring that is between a start and end point that we give it.

find_all()

Now we come to the find_all() function which is very useful. It makes use of the very popular preg_match_all() function to find all occurrences of a pattern in between a starting and ending delimiter. The real meat of this one is the pattern that gets passed into preg_match_all(). We can see the first argument is the pattern consisting of "($start(.*)$end)siU". This takes our start point, captures anything and everything multiple times(.*), denoted by the dot and asterisk operators, stops at the end point, performs a case insensitive match (i), excludes line breaks (s), and is not greedy (U). These modifiers are extremely important for the RegEx to work properly.

delete()

The delete() function is useful for removing one, or many substrings from within a string. We simply pass it a string, a start point, and an end point, and the function will take care of the rest for us!


Using Our New Helper Functions

Ok, we now have our helper functions defined, but how do we use them? Well, we can create a file called Stringhelpers.php and dump all of the code into it just like this:

Now when we want to use any of these functions we can simply include this file like so:

and we will have access to our new functions! Let’s test it out. We’ll create a new file called stringtest.php, include our helper file, and just call each new function on a small piece of text. Let’s see the results:

Wow! That was really cool! Now what’s the big deal with all this Laravel we’ve been talking about, then shifting gears and creating some simple PHP string helper functions?! Listen now Grasshopper, there is a method to our madness! You see before frameworks, this is how we did things. We created functions, we put them in files, we included those files into other files, and tada!, WordPress was born 🙂 Just kidding – but in all seriousness, we all probably have a lot of great functions and snippets of code in our arsenal. Once we start using a great framework like Laravel, do we have to abandon those old functions that we’ve come to know and love? But of course not! Now that we have created a file containing a handful of helper functions, we will have something to work with for our next tutorial. In our next episode, we’ll take a look at how to convert our old file based functions, and put them into a dedicated class which can be added to a libraries folder in Laravel. Then we’ll set up autoloading of any libraries we want to add to our project, and we’ll be able to use the new hotness of Laravel, along with any of our own Library functions we’ve collected over the years!

Thank you for reading PHP String Helper FunctionsPlease do share using the buttons below!