New String Handling Techniques

Learn about new string functions that help edit the string at the beginning, middle, or end.

Something PHP developers deal with on a constant basis is having to check for sets of characters that appear at the beginning, middle, or end of a string. The problem with the current set of string functions is that they are not designed to deal with the presence or absence of a substring. Rather, the current set of functions is designed to determine the position of a substring. This, in turn, can then be interpolated in a boolean manner to determine the presence or absence of a substring.

The problem with this approach is summarized in a famous quote attributed to Sir Winston Churchill:

“Golf is a game whose aim is to hit a very small ball into an ever smaller hole, with weapons singularly ill-designed for the purpose.”

Let’s now have a look at three incredibly useful new string functions that address this issue.

The str_starts_with() method

The first function we examine is str_starts_with(). To illustrate its use, consider a code example where we’re looking for https at the beginning and login at the end, as illustrated in the following code snippet:

Get hands-on with 1200+ tech skills courses.