+

PHP Trim () function

Related Functions :
  • PHP | Ltrim() function
  • PHP | Rtrim() function
Syntax:
trim ($string, $charlist)
Parameters:The function takes one required parameter and one optional parameter, as shown in the above syntax and described below.
  • $string : This the parameter specifies the string from which the space and the predefined left and right characters should be removed
  • $charlist : this is an optional parameter that specifies the characters to be removed from the string. If not mentioned, all of the following characters will be removed:
    • "/ 0" - NULL
    • "/ T" - tab
    • "/ N" - newline
    • "/ X0B" - vertical tab
    • "/ R" - carriage return
    • ”is a regular space
  • Returned value:Returns the modified line, removing the space, and See also predefined characters on both sides of the string, left and right.The following programs illustrate the trim() function:Program 1:
    // PHP program to demonstrate the use of trim() // function if there is a second parameter 
    // removes predefined characters from
    // front and back $str = "Hello World!" ; echo trim ( $str , "Hell!" ); ?>
    Output:
    o World < / pre> Program 2:       
    // PHP program to demonstrate the use of trim()
    // function when the second parameter is missing $str = " engineer for engineer" ;  
    // removes all leading and trailing spaces echo trim ( $str ); ?>
    Output:
    engineer for engineer
    Link: http://php.net/manual/en/ function.trim.php
    Get Solution for free from DataCamp guru