Cleaning up data either from a user or other outside source is a critical task every web dev needs to know. Having clean data means you're less likely to be tripped up later down the line. Trimming data (also known as trim) is one way in PHP that you can do just that, by using the built-in PHP function called trim(). In this guide, we will explore what trim is, how trim works, and examples of trim in action with our free download PHP code snippets.
What is Trim in PHP
PHP is provided with a function known as trim. Using trim is a quick and easy way to remove/strip whitespace from strings. It works by searching the passed string and removes any whitespace it finds at either the start or end of that string. If you look at any open-source project where they're accepting user input from a user, somewhere you'll likely find the trim function in action. Not only does trim remove whitespace but it can be used to remove other types of characters.
What characters does Trim remove?
As well as the default whitespace getting stripped, we can also pass a string of other types of characters to get PHP to remove these from our string too. We can pass anything, but it is normally used to remove common escape sequences. These include tab, new lines, carriage returns, NULL-byte, or vertical tab characters. But it is also possible to remove anything from the beginning and end of a string. Let's start exploring the trim function with easy-to-follow PHP example code.
How do you remove whitespace?
Removing whitespace is PHP's trim default action, so let's see it in action. Passing the string "Hello World" with a space character a the start and end of the string, PHP will remove both of these spaces, when we don't pass the optional second parameter. A common gottya is thinking PHP will remove all spaces, and it doesn't. You'll notice the space between the two words is maintained. That's because it isn't at the start or end of the string, therefore will not be removed. PHP will remove all instances it finds even when there is more than one. In the second example, the string is padded with many whitespaces, but that doesn't matter because we know that PHP will remove all of them. It is a great way to clean a string, especially when you plan to use it in the future. We can also think of whitespace as a space, a character which by default you cannot see unless it is mixed with other characters. That's why removing unwanted spaces with PHP trim is a common task you repeat project after project.
trim(' Hello World ');
# Outputs
Hello World
trim(' Hello World ');
# Outputs
Hello World
How to trim other characters?
Sometimes we don't need to remove the default whitespace and instead need to remove other characters. Whilst these can be anything, to perform that task we need to make use of the second optional parameter in trim. Let's explore that now in the following PHP example. Here we're passing another string after our input string telling PHP what we would like removed. By providing the letter 'm' to PHP, the engine will remove that letter from the start and end of the string.
trim('mFoo Bar', 'm');
# Outputs
Foo Bar
Providing a custom set of characters to PHP that we would like removed, does mean that it won't by default remove whitespaces anymore. The general rule of thumb is to provide whitespace characters (ASCII 32 (0x20)) as well as you're intended character set. In this next example, we've first passed a space followed by the letter "m". In the new example whitespace and the letter 'm' are removed.
trim(' mFoo Bar', ' m');
# Outputs
Foo Bar
A common mistake to make with trim in PHP is case sensitivity. When removing anything, we always want to ensure that we're removing matches the input and because of this, when trimming characters from strings with trim, it is case-sensitive. Take the following example, passing a lowercase 'f' to trim will not remove the uppercase 'F' from the start of the string. That's understandable, as we've provided a lowercase 'f' to strip not an uppercase one. If we wanted to remove both uppercase and lowercase 'f' characters, we simply could provide them, as shown in the second example.
trim('Foo Bar', 'f');
# Outputs
Foo Bar
trim('Foo Bar', 'fF');
# Outputs
oo Bar
How trim will remove multiple characters
It might not be the first apparent when working with trim, but when passing a character set to trim, PHP will keep removing all matches from both the start and end, unless it finds no more. With whitespace removal, this is ideal, but with a character set, this might not be. Sometimes this can lead to undesirable results as shown in the following example. If we tell to remove the letter "l" from our string, PHP will without question remove it from both the start and end of our string.
trim('light label', 'l');
# Outputs
ight labe
Another process that the trim function will perform is removing the same character each time it finds it appears at the start or end of a string. Taking our 'l' example, by passing this letter to the function, PHP will remove all occurrences it finds at the start and end. Because removing the first 'l' from the end of the string, then results in another 'l' being at the end of the string, PHP will remove that too. In the second more radical example, passing "P" to trim will get all 'P's removed from it. Because PHP starts and ends with a capital 'P', the result is just 'H'.
trim('Fall', 'l');
# Outputs
fa
trim('PPPPPPPPPHPPPPPPPPP', 'P');
# Outputs
H
We can also remove numbers from a string with PHP. To do this we simply pass the numbers into the parameter as shown below. However, there is a better and cleaner way to do this.
trim('Foo Bar 124314', '1234567890');
# Outputs
Foo Bar
How to specify a range of characters to PHP's trim function
Taking our above example, it can be hard to read and is susceptible to mistakes when passing many characters into the function. However, PHP provides a cleaner and better way to overcome this by using the range function, double dots "..". We can instead put a range of numbers in the parameters to achieve the same result. By passing the "0..9", we are telling PHP we want all numbers from 0 to 9 (yes inclusive) to be removed. You can now see that this is much easier to read than "0123456789". And if we don't want to include the inclusive numbers, maybe in this example we would like to keep the number 1, we simply need to ensure we don't include it as shown in our second example, where we've updated the range to "2..9".
trim('Foo Bar 124394', '0..9');
# Outputs
Foo Bar
trim('Foo Bar 124394', '2..9');
# Outputs
Foo Bar
If you are looking to only trim the start or only trim the end of a string using ltrim or rtrim may be a safer option than trim. That's because trim removes from both the start and end, whereas, you may be looking to just remove from one rather than both.
Conclusion
Trim is a powerful PHP function that can help keep your data clean, which is especially important when processing data from outside your ecosystem, such as user input data. Whilst the most common form of trimming is to remove whitespace off strings, using trim to remove other characters is a powerful way to quickly and easily keep your data on-point, just the way to want it.
- Remember trim is case-sensitive
- Trim will remove all occurrences of characters it finds from both start and end
- Use the range character ".." to specify a range of characters to be removed to create easier-to-read code
- Trim will not remove characters from the middle of a string, only start and end
- Use left trim (ltrim) or right trim (rtrim) if you want to remove only from one side