w3resource

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.
"\0" - the NUL-byte.
"\t" - tab.
"\n" - line feed.
"\x0B" - a vertical tab.
"\r" - a carriage return.
Optional String

Return value:

The trimmed string.

Value Type: String.

Pictorial Presentation

string_trim

Example - 1:

<?php
$string_name=' Welcome to w3resource.com ';
$new_string=trim($string_name);
echo $new_string;
?>

Output:

Welcome to w3resource.com

View the example in the browser

Example - 2 :

<?php
$string_name="	Welcome to w3resource.com";
$new_string=trim($string_name,"\t");
//echo $new_string;
var_dump($new_string);
?>

Output :

string(25) "Welcome to w3resource.com"

View the example in the browser

See also

PHP Function Reference

Previous: substr
Next: ucfirst



Inviting useful, relevant, well-written and unique guest posts