w3resource

PHP: trim() function

PHP: Strip whitespace (or other characters) from the beginning and end of a string

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



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php/function-reference/trim.php