How to Trim Strings in PHP

There are a lot of times when we want to work with strings but they are padded with unwanted characters either at the beginning or the end. PHP has defined specific functions that we can use to trim such strings easily.

In this quick tip, we will discuss to common string trimming situations that you will likely encounter. The first one involves removing specific characters from either one or both ends of a string. The second one involves trimming strings based on how many characters we want to remove from either end.

Trimming Strings to Remove a Set of Characters

There are three different trimming functions in PHP called ltrim(), rtrim() and trim(). They remove either whitespace or any other set of characters from the left end, right end or both ends respectively.

If you don’t mention as list of characters to remove as the second parameter, these function will remove whitespace characters like spaces (" "), tabs ("t"), new lines ("n"), carriage returns ("r"), null bytes ("") and vertical tabs ("v").

There is no limit to the number of characters that will be trimmed by these functions. In other words, they will keep trimming the string as long as it contains any of the specified characters at either end.

Here are a few examples of these functions. The main strings that we are trimming will contain a combination of whitespace, tabs or newline characters at both ends.

As you can see, ltrim() removes all the whitespace form the left of the strings while keeping the right end untouched. Similarly, rtrim() removes all the whitespace from the right of the strings while keeping the left end untouched.

It is important to keep in mind that trimming only happens when the characters are exactly at the end or beginning. Here is an example:

You can also trim any other set of characters if you pass them as second parameter of ltrim(), rtrim() or trim().

This time we had specified that we only want to trim the characters 1,2 and 3. Therefore, trim() left the whitespace untouched.

Similarly, in this second example, trim stopped removing digits from the both ends as soon as it found a mismatch from the characters we asked it to trim.

This third example demonstrates how we can use (..) to specify a range of characters that we want trimmed. This way we can avoid listing all the characters if they occur sequentially.

Trimming Strings to Remove a Specific Number of Characters

Just like trimming a specific set of characters, PHP also has dedicated functions to trim a specific number of characters.

The substr($string, $offset, $length) function is best suited for this job. It accepts three parameters and returns a substring. The first parameter is the string you want to trim. The $offset parameter determines the start of the returned substring and the $length parameter determines the length of the substring.

We can control the values of $offset and $length to trim our main string based on number of characters.

  1. You can trim the string from the left side by only specifying an offset and omitting the $length parameter when calling substr(). In this case, substr() will return a substring starting from offset until the end of the string.
  2. You can trim the string from the right side by setting the value of $offset to 0 and making $length negative. This will remove $length number of characters from the right end of the main string.
  3. You can specify a non-zero positive value for $offset and a negative value for $length to make substr() act like trim() and remove a specific number of characters form both ends.

Here is an example that trims a string using the substr() function:

Final Thoughts

In this quick tip, we learned how to trim strings in PHP based on a specific set or a specific number of characters. If you have any questions or tips related to the post, feel free to post them in the comments.

Learn PHP With a Free Online Course

If you want to learn PHP, check out our free online course on PHP fundamentals!

 

In this course, you’ll learn the fundamentals of PHP programming. You’ll start with the basics, learning how PHP works and writing simple PHP loops and functions. Then you’ll build up to coding classes for simple object-oriented programming (OOP). Along the way, you’ll learn all the most important skills for writing apps for the web: you’ll get a chance to practice responding to GET and POST requests, parsing JSON, authenticating users, and using a MySQL database.

Leave a comment

Your email address will not be published.