What does php trim do?
In this tutorial, we look at the PHP trim function. We look at how you can remove whitespaces and characters from a string using the trim function in PHP. Show
Table of Contents - PHP Trim:
What does the PHP trim() function do?The
Code & Explanation:In this section,
we look at the various syntax, parameters, and return values used in the Syntax:
Parameters
Return Value:A modified string with whitespaces or the specified characters removed from both sides is returned. Code using PHP Trim:
The output for the above code snippet would be as follows:
Now let’s look at a case where we pass a second argument.
The output for this code snippet would be the following:
As you can see the “hir” from “Hire” and the “r” from “developer” were removed. Closing thoughts - PHP trim():There are no caveats as such while using the PHP trim() function. I would recommend practicing the Other TutorialsWork with top startups & companies. Get paid on time.
Try a Top Quality PHP Developer for 14 Days. Pay Only If Satisfied.
(PHP 4, PHP 5, PHP 7, PHP 8) trim — Strip whitespace (or other characters) from the beginning and end of a string Descriptiontrim(string
Parametersstring The string that will be trimmed. characters Optionally, the stripped characters can also be specified using the Return ValuesThe trimmed string. ExamplesExample #1 Usage example of trim()
= "\t\tThese are a few words :) ... "; The above example will output: string(32) " These are a few words :) ... " string(16) " Example string " string(11) "Hello World" string(28) "These are a few words :) ..." string(24) "These are a few words :)" string(5) "o Wor" string(9) "ello Worl" string(14) "Example string" Example #2 Trimming array values with trim()
The above example will output: array(3) { [0]=> string(5) "apple" [1]=> string(7) "banana " [2]=> string(11) " cranberry " } array(3) { [0]=> string(5) "apple" [1]=> string(6) "banana" [2]=> string(9) "cranberry" } Notes
See Also
There are no user contributed notes for this page. Does PHP trim Remove newline?Yes it does, see the manual: This function returns a string with whitespace stripped from the beginning and end of str .
What is the use of trim in string?Overloads. What does trim () do in angular?The trim() method removes whitespace from both sides of a string. The trim() method does not change the original string.
What is whitespace in PHP?A whitespace is any character that renders as a space, that is: A space character. A tab character. A carriage return character.
|