trim

string trim(string string) 

Removes whitespace from the left and right ends of a string.

Returns:

String

Description:

trim() removes all whitespace characters from both the left and right ends of a string. The function considers the following characters to be whitespace:

Character Name ASCII Value PHP Character Escape Sequence
null 0 \0
tab 9 \t
newline 10 \n
vertical tab 11 none
carriage return 13 \r
space 32 none

Version:

PHP 3+, PHP 4+

See also:

To strip whitespace from the start or end of a string:

chop() 
ltrim() 
rtrim() 

Example:

Ensure that a value submitted contains characters other than whitespace
 <?php if (count ($HTTP_POST_VARS)) { $name = trim ($HTTP_POST_VARS['name']); if ($name) { echo ("<b>$name</b>: There are some who ...

Get PHP Functions Essential Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.