PHP trim() Function

Syntax : trim(string $string, string $characters)

The Php trim() Function is an inbuilt function which removes whitespaces and also the predefined characters from both sides of a string that is left and right.

The Php trim() Function is an inbuilt function which removes whitespaces and also the predefined characters from both sides of a string that is left and right.

$string specifies the string from which the whitespace and predefined characters from left and right are to be removed

$charlist specifies the characters that are to be removed from the string.

lrim() Function removes whitespaces from left side of a string.

rtrim() Function removes whitespaces from right side of a string.

Example 1 :

 

<?php  

$str = "  Welcome Aone Tutorials  "; 

$newstr = trim($str);

echo $newstr;

?>  

Output : 

Welcome Aone Tutorials

Example 2 :

The lrim() function removes whitespace or other predefined characters from the left side of a string.

 

<?php

$str = " Hello World";

echo lrim($str);

//output : Hello World.

?>

Example 3 :

The rtrim() function removes whitespace or other predefined characters from the right side of a string.

 

<?php

$str = "Hello World  ";

echo rrim($str);

//output : Hello World.

?>