php trim- Tutorialpath

Welcome to all our readers on our website at As you all know that here on our website we will provide you lots of information related to the latest and newly introduced technology which help you to get updated with the latest tech. Here you will get all the useful information which is required in this tech world. As you know technology is one of the trending part in this world. So, here again we came up with a latest and new tech information which is related to the process of creating php trim() Function. With the help of this article you will get to know the complete process of how to create php trim() Function in a step by step manner. So, simply have a look to this article and grab all the useful information which is going to be very helpful for you.

PHP trim() Function

PHP Trim() Function
PHP Trim() Function

The trim () function in PHP is an inbuilt function that removes white spaces and leaves predefined characters from both sides of a string that are right and left.


trim(string_name, char_list)


The function accepts a mandatory parameter and an optional parameter as shown in the syntax above and described below.

  1. $string: This parameter specifies the string from which to delete white space and predefined characters from left and right.
  2. $charlist: This is an optional parameter that specifies the characters to be removed from the string. If it is not mentioned, all the following characters will be removed

Name Description Required /Optional Type
string_name The string to be trimmed. Required String
char_list Specifies the character(s) to remove. Without this remove the following ” ” an ordinary space.
“\0” – the NUL-byte.
“\t” – tab.
“\n” – line feed.
“\x0B” – a vertical tab.
“\r” – a carriage return.
Optional String

Return Value: It returns the modified string by removing the white space and predefined characters on either side of the string that are left and right.

Program :

// PHP program to demonstrate the use of trim()
// function when second parameter is present// removes the predefined characters from
// front and back$str = “Hello World!”;
echo trim($str, “Hello!”);



Method First – substr function

            You can use the substr function of php to extract the last character from a string in php.


substr($string, 0, -1);

Method Second – substr_replace function

            You can also use substr_replace to remove the last character from a string in Php.


substr_replace($string ,"", -1);

Method Third – rtrim() function

You can use the PHP rtrim() function to remove the last character from a given string in PHP.



Question:- php remove last character from string if comma

If you have a comma separeted string in php and you want to remove last character from string if comma, so you can use php rtrim() function like below:

$string = "Hi, Tutorialpath,";
 echo "Given string: " . $string . "\n";
 echo "Updated string: " . rtrim($string, ",") . "\n";


  Given string: Hi, Tutorialpath,
  Updated string: Hi, Tutorialpath



So, hopefully we have shared all the basic steps that help you php Trim Function.  PHP extract the last character from a string. In this tutorial, you have several ways for PHP to remove the last character from any given strings.


Leave a Reply

Your email address will not be published. Required fields are marked *