In programming-speak, trimming a string means removing unwanted characters — typically whitespace characters — from either end (or both ends) of the string. This is useful in a number of scenarios, such as:
- Cleaning up user input, such as text field values in a submitted form
- Removing unwanted line-break characters at the end of a line
- Ensuring that a string is in the correct format for passing to another program
In this article you explore 3 useful PHP functions that you can use to trim strings:
trim()
for trimming both ends of a stringltrim()
for trimming the beginning of a string, andrtrim()
for trimming the end of a string.