All trimming functions are used to remove any whitespaces from the starting and end of the string. The following characters are considered as whitespace in PHP. The particular character or range of characters can be used without these whitespace characters. Some commonly used characters that can be used for trimming have been mentioned below.
Character | Purpose |
---|---|
” “ | It defines the space character. |
“\n” | It defines the newline character. |
“\t” | It defines the tab space. |
“\r” | It defines the carriage return character. |
“\0” | It defines the vertical tab character. |
“\x0B” | It defines the null-byte character. |
Use of trim() Function
The trim() function can take two inputs as argument values. The first argument is mandatory that contains the main string value and the second argument is optional and it contains the specific character or the list of characters that will be used to trim the string value. It returns the string after trimming. The syntax of this function is shown below.
If the optional argument is omitted, then the function will remove space from the starting and ending part of the value provided by the first argument.
Example-1: Trim String Based on the Space
Create a PHP file with the following script. The uses of trim() function without the optional argument has been shown in the following example. The string, ‘***’ has been added before and after the string to check how trim() function works.
//Define a string value
$str = " I like programming ";
echo "<b>The string value before trimmimg:</b><br/>***$str***";
//using trim() function without optional value
echo "<br/><b>The string value after trimming:</b><br/>***".trim($str)."***<br/>";
?>
Output:
The following output will appear after executing the above script. The output shows that the space from both sides of the string has been removed.
Example-2: Trim String Based on Specific Characters
Create a PHP file with the following script. The ‘<br/>’ exists in both side of the main string value and ‘<br/>’ has been used as the optional argument value to remove ‘<br/>’ from both side of the main string after using trim() function.
//Define a string value with '<br/>'
$str = "<br/>PHP is a popular programming language<br/>";
echo "<b>The string value before trimmimg:</b>$str";
//using trim() function with '<br/>' as optional argument
echo "<br/><b>The string value after trimming:</b>".trim($str, "<br/>");
?>
Output:
The following output will appear after executing the above script. The output shows that the newline has been removed from both sides of the string.
Example-3: Trim String Based on the Range of Characters
Create a PHP file with the following script. Here, the numeric values have used on both side of the string and the numeric range 0..9 has been used as the optional argument value of trim() function to remove numeric part from both side of the string.
//Define a string value
$str = "123 linuxhint.com 890";
echo "<b>The string value before trimmimg:</b><br/>$str";
//using trim() function with numeric range as optional value
echo "<br/><b>The string value after trimming:</b><br/>".trim($str, "0..9")."<br/>";
?>
Output:
The following output will appear after executing the above script. The output shows that the numbers have been removed from both sides of the string.
Use of ltrim() Function
The ltrim() function works like the trim() function but it removes whitespaces or other specific characters from the left side of the main string The syntax of this function is shown below.
Example-4: Trim Characters from the Left Side of the String
Create a PHP file with the following script. Like the previous example, the ‘***’ has been added before and after the main string to check how the ltrim() function works without the optional argument. Next, the numeric range 0..9 has been used as the optional argument value of the ltrim() function to remove the numeric part from the left side of the string.
//Define string by adding space on both sides of the string
$Str1 = " PHP is a popular language ";
//Define string by adding numbers on both sides of the string
$Str2 = "02JavaScript is client-side scripting language99";
//using ltrim() function
echo "<b>The original string value:</b><br/>***$Str1***<br/>";
echo "<b>The string value after using ltrim() without optional value: </b><br/>***". ltrim( $Str1 )."***<br/><br/>";
echo "<b>The original string value:</b><br/>$Str2<br/>";
echo "<b>The string value after using ltrim() with optional value:</b><br/>". ltrim( $Str2, "0..9" )."<br/>";
?>
Output:
The following output will appear after executing the above script. The output shows that the space has been removed from the left side of the string when the ltrim() has been used without the optional argument. The ‘ #’ has been removed from the left side of the string when the ltrim() has been used with the optional argument.
Use of ltrim() Function
The rtrim() function works like the trim() function but it removes whitespaces or other specific characters from the right side of the main string The syntax of this function is shown below.
Example-5: Trim Characters from the Right Side of the String
Create a PHP file with the following script. Like the previous example, the ‘***’ has been added before and after the main string to check how the rtrim() function works without the optional argument. Next, the string, ‘ #’ has been used as the optional argument value of the rtrim() function to remove ‘ #’ from the right-side of the string.
//Define string by adding space on both sides of the string
$Str = " ##Welcome to LinuxHint## ";
//using rtrim() function
echo "<b>The original string value:</b><br/>$Str<br/>";
echo "<b>The string value after using rtrim() without optional value: </b><br/>***". rtrim( $Str )."***<br/>";
echo "<b>The string value after using ltrim() with optional value:</b><br/>". rtrim( $Str, " #" )."<br/>";
?>
Output:
The following output will appear after executing the above script. The output shows that the space has been removed from the right side of the string when the rtrim() has been used without the optional argument. The ‘ #’ has been removed from the right side of the string when the rtrim() has been used with the optional argument.
Conclusion
Three different ways to trim string data in PHP have been shown in this tutorial by using multiple examples for helping new PHP users to understand the uses of trimming functions properly.