The trim() function in PHP is an inbuilt function which removes whitespaces and also the predefined characters from both sides of a string that is left and right.
Related Functions:
Syntax:
trim($string, $charlist)
Parameters:The function accepts one mandatory parameter and one optional parameter as shown in the above syntax and described below.
- $string: This parameter specifies the string from which the whitespace and predefined characters from left and right are to be removed
- $charlist: This is an optional parameter which specifies the characters that are to be removed from the string. If this is not mentioned then all of the following characters will be removed:
- “\0” – NULL
- “\t” – tab
- “\n” – new line
- “\x0B” – vertical tab
- “\r” – carriage return
- ” ” – ordinary white space
Return Value: It returns the modified string by removing the whitespace and also the predefined characters from both sides of a string that is left and right.
Below programs illustrate the trim() function:
Program 1:
<?php
$str = "Hello World!" ;
echo trim( $str , "Hell!" );
?>
|
Output:
o World
Program 2:
<?php
$str = " geeks for geeks " ;
echo trim( $str );
?>
|
Output:
geeks for geeks
Reference: http://php.net/manual/en/function.trim.php
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!