.


Divya

trim() function in php


The trim() function is used to removes whitespace and other predefined characters from both sides of a string in PHP.

Example

<!DOCTYPE html>
<html>
<body>
<?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hello"). "<br>";
?>
</body>
</html>

Related function to trim() function

1.ltrim() Function

The ltrim() function is used to removes whitespace or other predefined characters from the left side of a string.

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$str = "Students Tutorial";
echo $str . "<br>";
echo ltrim($str,"Students"). "<br>";
?>
</body>
</html>

2.rtrim() Function

The rtrim() function is used to removes whitespace or other predefined characters from right sides of a string.

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$str = "Students Tutorial";
echo $str . "<br>";
echo rtrim($str,"Tutorial"). "<br>";
?>
</body>
</html>


.