trim() function in PHP

PHPProgrammingServer Side Programming

The trim() function is used to remove whitespaces and other characters.

Syntax

trim(str, charlist):

Parameters

  • str − Specify the string to check.

  • charlist − Specifies which characters to remove. If this parameter isn’t used, then any of the following characters are removed −

    • "\0" - NULL

    • "\t" - tab

    • "
      " - new line

    • "\x0B" - vertical tab

    • "\r" - carriage return

Return

The trim() function returns the modified string.

Example

The following is an example −

 Live Demo

<?php
   $str = "

Demo text!

";    echo "Trim: " . trim($str); ?>

Output

The following is the output −

Trim: Demo text!

Example

Let us see another example −

 Live Demo

<?php
   $mystr = "Demo text!";
   echo trim($mystr, "Dam!");
?>

Output

The following is the output −

emo text
raja
Updated on 26-Dec-2019 10:15:19

Advertisements