Showing posts with label trim function. Show all posts
Showing posts with label trim function. Show all posts

Saturday, September 13, 2008

trim() function

The trim() function could be used to remove certain pre-defined characters from both ends of a string.
Syntax: trim(string, [list of characters]);
string- the string in which the predefined characters are to be removed.
list of characters.
\0 for NULL
\t for TAB
\n for new line
“ ” for whitespace
If the list of characters are not specified, all pre-defined characters would be removed.
Example inputs
1.
$first_name = “James ”;
echo $first_name.”
”;
trim($first_name);
echo first_name;
In the output, the second echo function prints $first_name that has one less space.

2.
$name = “John
”;
echo $name.”
”;
trim($name);
echo $name;
In this case, the second time $name is printed, the new line character is absent.