PHP trim() Function
In this chapter you will learn:
- Definition for PHP trim() Function
- Syntax for PHP trim() Function
- Parameter for PHP trim() Function
- Return for PHP trim() Function
- Note for PHP trim() Function
- Example - Trim a string
Definition
The trim()
function strips spaces, new lines, and tabs from either side of a string.
Syntax
PHP trim() Function has the following syntax.
string trim ( string str [, string trim_chars] )
Parameter
The optional second parameter to trim() sets the individual characters you want it to trim.
Return
PHP trim() Function returns the trimmed string value.
Note
There are two minor variants to trim()
.
ltrim()
and rtrim()
do the same thing,
but only trim from the left and right respectively.
Example
Trim a string
<?PHP/* j a v a 2s . c om*/
$a = trim(" testing from java2s.com ");
print $a;
print "\n";
$b = trim(" testing from java2s.com", "com");
print $b;
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP ucfirst() Function
- Syntax for PHP ucfirst() Function
- Parameter for PHP ucfirst() Function
- Return for PHP ucfirst() Function
- Example - Convert the first letter to uppercase
Home » PHP Tutorial » PHP String Functions