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

    • "
      " - 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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 26-Dec-2019

230 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements