Core PHP

PHP trim() Function With Multiple Examples

PHP trim() function is mainly used to trim or remove whitespace from the start and end of the string. We can also...

Written by Bikash · 1 min read >
PHP trim() Function With Multiple Examples

PHP trim() function is mainly used to trim or remove whitespace from the start and end of the string. We can also use it in various ways which we learn from this complete article.

PHP trim() function is an inbuilt PHP function. There are also two types of trim functions,

  • rtrim removes whitespaces and other specified characters from the right side of the string.
  • ltrim removes whitespaces and other specified characters from the left side of the string.

Syntax

The syntax trim()function accepts two parameters,

  • $string is the string that we want to trim.
  • $characters is an optional argument, used to remove specific characters from beginning and end of the string.

The output of the trim function is also a String.

There are also some default characters that the trim function removes,

CharacterASCIIHexDescription
” “320x20a space
“\t”90x09a tab
“\n”100x0Aa new line
“\r”130x0Da return
“\0”00x00a NUL-byte
“\v”110x0Ba vertical tab
Trim Function Default Characters

If you want to remove some specific characters, you can use the above as a second argument in the trim function.

Things, which we discuss on the PHP trim() function

  1. Remove spaces before and after from the string.
  2. PHP trim() function to remove specified characters.
  3. Use PHP trim with arrays.
  4. PHP trim the last character.
  5. Trim the comma from the end of the string.

Remove spaces before and after from the string using PHP trim()

Output:

Remove spaces before and after from the string using PHP trim()
Remove spaces before and after from the string using PHP trim()

Here we define a string with spaces at the start and end.

And then we use the trim function to remove whitespaces from both sides.

Also Read: 5 Most Useful PHP String Functions You Should Know

PHP trim() function to remove specified characters

When you run the below code, the trim function checks the character on both sides and removes the specified characters.

Output:

o World

PHP trim() function to remove specified characters
PHP trim function to remove specified characters

Use PHP trim With an Arrays

In the above PHP trim array example, first, we create a function and take the array value as an argument.

Then we define an array of elements with some spaces.

Then we use the array_walk function to call the function with the array as an argument.

Output:

Use PHP trim With an Arrays
Use PHP trim With an Arrays

PHP trim last character of the String (rtrim)

PHP rtrim() function is used to remove characters from the right side of the string.

Output:

Original string: Hello PHPCoder!
Updated string: Hello PHPCoder

PHP trim last character of the String (rtrim)
Trim the last character of the String using PHP(rtrim)

Also Read: PHP file_get_contents() Function With Example

PHP trim comma from the end of the string

Output:

phpcoder

Conclusion

This is the complete article and tutorial on the PHP trim() function with help of multiple examples. Here we can see the complete source code of each example and the types of trim functions.

I hope you all understand the complete source code.

If you are facing any issues please let me know in the comment below.

Happy Coding..!

Was this article helpful?
YesNo
Written by Bikash
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP. Profile

Leave a Reply

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