PHP trim

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP : trim() function

Description

The trim() function is used to remove the white spaces and other predefined characters from the left and right sides of a string.

Version

(PHP 4 and above)

Syntax

trim(string_name, char_list)

Parameters

Name

Description

Required /
Optional

Type

string_name

The string to be trimmed.

Required

String

char_list

Specifies the character(s) to remove. Without this remove the following " " an ordinary space.
"�" - the NUL-byte.
" " - tab.
" " - line feed.
"x0B" - a vertical tab.
" " - a carriage return.

Optional

String

Return value

The trimmed string.

Value Type : String.

Pictorial Presentation

string_trim

Example - 1 :

  1. $string_name='  Welcome to w3resource.com  ';  

  2. $new_string=trim($string_name);  

  3. echo $new_string;  

  4. ?>  

Output :

Welcome to tutorialtours.com

Example - 2 :

  1. $string_name="  Welcome to w3resource.com";  

  2. $new_string=trim($string_name," ");  

  3. //echo $new_string;  

  4. var_dump($new_string);  

  5. ?>  

Output :

string(25) "Welcome to tutorialtours.com"