What is PHP trim() Function?
If you want to remove whitespaces and other characters from both sides of a string, use trim() function. It is very important to remove whitespaces from a string. For example, suppose, a user can add a space at the end of the username. When he’ll try to login later, he can’t because he will just try with the username except the space.
Note, a whitespace is any character that renders a space. It includes – an ordinary space character, a tab character, a new line (line feed) character, a carriage return character, a NUL-byte character, a vertical tab character.
Syntax:
trim(string, characters)
Parameters:
The Function has 1 required and 1 optional parameter-
string (Required): The string that is to be trimmed. Check example 1. If you don’t mention the second parameter, the trim() function actually removes the following characters-
- An ordinary space – “ ” which is
- A tab – “\t”
- A new line (line feed) – “\n”
- A carriage return – “\r”
- The NUL-byte – “\0”
- A vertical tab – “\t”
characters (Optional): A list of characters that is to be removed from the “string” parameter.
- Mention all the characters that you want to trim from the string. Check example 2.
- If you want to remove a range of characters, use “..”. Ex. “1..5”, “A..M” etc. For example, if you want to remove all the character from a to m, specify “a..m”. Check example 3.
Return Values:
The function returns the trimmed string.
How trim() function works:
When using with just one parameter (the string), the function removes any space, tab, new line, carriage return, NUL-byte, vertical tab from both sides of the string.
When using with the second parameter (the characters), it removes all the characters from both end of the first parameter. The function removes the characters listed in second parameter recursively from both sides of the string as long as it finds any mismatching characters. Check example 4.
Examples:
Example 1: Trimming whitespaces with trim() function-
<pre>
<?php
$string = " Schools of web ";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = trim($string);
echo "After trimming: '$afterTrimming'";
?>
</pre>
Output:
Before trimming: ‘ Schools of web ‘
After trimming: ‘Schools of web’
Explanation:
When the trim() function uses just one parameter, it removes all the whitespaces from the string.
Example 2: Trimming specific characters with trim() function-
<pre>
<?php
$string = "Schools of web";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = trim($string, "Scb");
echo "After trimming: '$afterTrimming'";
?>
</pre>
Output:
Before trimming: ‘Schools of web’
After trimming: ‘hools of we’
Explanation:
It removes capital “S”, “c”, and “b” from the string.
Example 3: Trimming a range of characters with trim() function-
<pre>
<?php
$string = "learn web development at Schools of web";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = trim($string, "a..l");
echo "After trimming: '$afterTrimming'";
?>
</pre>
Output:
Before trimming: ‘learn web development at Schools of web’
After trimming: ‘rn web development at Schools of w’
Explanation:
It removes all the characters from “a” to “l”.
Example 4: trim() function trimming characters recursively-
<pre>
<?php
$string = "aoa Schools of Web ou";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = trim($string, "aou");
echo "After trimming: '$afterTrimming'";
?>
</pre>
Output:
Before trimming: ‘ao Schools of Web ou’
After trimming: ‘ Schools of Web ‘
Explanation:
Three characters “a”, “o”, and “u” are to remove from the string. At first, it removes “a” from the beginning of the string. After that, “o” becomes the first character of the string and the function again trim this character and continues this process.
Example 5: Trimming similar characters from a pile of data with trim() function-
<pre>
<?php
$languages = array("1994PHP", "1991Python", "1995Ruby");
for($i=0; $i<count($languages); $i++){
echo "Before trimming: '$languages[$i]'";
$afterTrimming = trim($languages[$i], "0..9");
echo " After trimming: '$afterTrimming'" . "<br />";
}
?>
</pre>
Output:
Before trimming: ‘1994PHP’ After trimming: ‘PHP’
Before trimming: ‘1991Python’ After trimming: ‘Python’
Before trimming: ‘1995Ruby’ After trimming: ‘Ruby’
Explanation:
trim() function can remove similar character pattern from a large pile of strings.
Example 6: Trimming whitespaces from the second parameter with trim() function-
<pre>
<?php
$string = " Schools of web\t ";
echo "Before trimming: '$string'";
$afterTrimming = trim($string, "a");
echo " After trimming: '$afterTrimming'";
echo "<br />";
echo "Before trimming: '$string'";
$afterTrimming = trim($string, " \t");
echo " After trimming: '$afterTrimming'";
?>
</pre>
Output:
Before trimming: ‘ Schools of web ‘ After trimming: ‘ Schools of web ‘
Before trimming: ‘ Schools of web ‘ After trimming: ‘Schools of web’
Explanation:
When you use second parameter, the trim() function doesn’t remove the whitespaces automatically unless you mention these in the second parameter. So, the function in line 5 doesn’t remove whitespaces, but, it does in line 9.
Example 7: Case sensitive trimming with trim() function-
<pre>
<?php
$string = "Schools of web";
echo "Before trimming: '$string'";
$afterTrimming = trim($string, "s");
echo " After trimming: '$afterTrimming'";
echo "<br />";
echo "Before trimming: '$string'";
$afterTrimming = trim($string, "S");
echo " After trimming: '$afterTrimming'";
?>
</pre>
Output:
Before trimming: ‘Schools of web’ After trimming: ‘Schools of web’
Before trimming: ‘Schools of web’ After trimming: ‘chools of web’
Explanation:
The function removes characters case-sensitively. So, if you mention small letter “s” in the second parameter to remove, the function won’t remove capital “S” from the string.
Example 8: Trimming multiple characters, whitespaces together with trim() function-
<pre>
<?php
$string = "1994 Schools of web\t";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = trim($string, "1..9, ' ', \t");
echo "After trimming: '$afterTrimming'";
?>
</pre>
Output:
Before trimming: ‘1994 Schools of web ‘
After trimming: ‘Schools of web’
Explanation:
To remove multiple types of characters like whitespaces, normal characters, numbers, you’ll use comma to separate those in the second parameter.
Practical Usages of trim() Function:
Few practical usages of the trim() function includes-
- The function is very useful to sanitize user inputs. It eliminates unnecessary spaces, tabs, carriage returns, newline etc. from user inputs. For example, users may add extra space when supplying username or users sometimes add line break at the end of a comment. This function helps to trim these in the server side.
- The function is also very useful to sanitize data that are found from external files, database etc.
- To trim similar types of characters from the both sides of the string, you can use trim() function. For example, you have a large excel file which has a string column. Each string of the column contains few number on its both side. You use trim() to easily remove numbers. Check example 5.
Notes on trim() Function:
- When you mention the second parameter, the trim() function doesn’t remove the whitespaces from the string automatically. You’ve to mention in the second parameter the list of whitespace characters you want to remove. Check example 6.
- The characters you mention in the second parameter work case-sensitively. Which means, if you enlist “S” (Capital letter), the function will remove “s” (Small letter) not “S” (Capital letter) from string. Check example 7.
- You can mention characters, multiple whitespace characters in the second parameter with comma. Check example 8.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP trim() Function
trim() is one of the frequently used built-in PHP string functions. By removing extra spaces, tabs etc., it ensures data consistency and makes coding smooth.