Facebook Page Twitter Page LinkedIn Page
× PHP Tutorial PHP Advanced PHP Resources


The PHP trim() function is used to return a string which is trimmed version of the specified string. It removes whitespaces from both end of the specified string. This function has one optional parameter which is used to specify trim character(s). If nothing is specified then the function takes default value which is whitespace.

Syntax

trim(string, character(s))

Parameters

string Required. Specify the string to convert
Character(s) Optional. Character(s) which need to be trimmed from the string. default value is whitespace.

Return Value

Returns the trimmed version of the specified string.

Example:

In the below example, trim() function to used to return the trimmed version of the string MyString.

<?php
  $MyString = "    Hello World!.    ";
  $NewString = trim($MyString);

  echo $NewString."\n";
?>

The output of the above code will be:

Hello World!.

Example:

We can also specify the characters which need to be trimmed from the string as shown in the below example.

<?php
  $MyString = ",*#Hello World!#@!";
  $NewString = trim($MyString, ",*#@!");

  echo $NewString."\n";
?>

The output of the above code will be:

   
Hello World

❮ PHP String functions