Home >>PHP String Functions >PHP trim() Function
PHP trim() function is used to remove whitespaces and also the other pre-defined characters from both sides of a given input string. It also eliminates the leading and the trailing spaces.
Syntax:
trim($string, $charlist);
Parameter | Description |
---|---|
string | This is a required parameter. This parameter defines the string to check. |
charlist | This is an optional parameter. This parameter defines which characters to remove from the string. |
Here is an example of trim() function in PHP:
<html> <body> <?php $str = "Good Morning Everyone!!!"; echo $str . "<br><br>"; echo trim($str,"Good"); ?> </body> </html>
Here is an another example of trim() function in PHP:
<html> <body> <?php $str = "Good Morning Everyone!!!"; echo $str . "<br><br>"; echo trim($str,"Good!!"); // trim "Good" from the start and "!!!" from the end ?> </body> </html>