Computer >> Computer tutorials >  >> Programming >> PHP

trim() function in PHP


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

    • "\n" - new line

    • "\x0B" - vertical tab

    • "\r" - carriage return

Return

The trim() function returns the modified string.

Example

The following is an example −

<?php
   $str = "\n\nDemo text!\n\n";
   echo "Trim: " . trim($str);
?>

Output

The following is the output −

Trim: Demo text!

Example

Let us see another example −

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

Output

The following is the output −

emo text