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 function,

  • 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

According to the syntax trim()function accepts two parameters,

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

The output of the trim function is also a String.

There are also some default characters that 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 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 an arrays.
  4. PHP trim last character.
  5. Trim comma from 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 on 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

On the above PHP trim array example, first, we create a function and takes 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)
PHP trim last character of the String (rtrim)

Also Read: PHP file_get_contents() Function With Example

PHP trim comma from end of string

Output:

phpcoder

Conclusion

This is the complete article and tutorial of 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 function.

I hope you all understand the complete source code.

If you are facing any issue please let know on the comment below.

Happy Coding..!

Was this article helpful?
YesNo
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH, my native place in Odisha. I am a Web Programmer by profession and working on more than 50 projects to date. My passion is working on the web-based project using PHP and relate to all CMS and frameworks which is based on PHP.
Posts created 181

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
mariobet - supertotobet - escort - milanobet giriş - gizabet giriş -
perabet giriş
- betkanyon giriş -

jojobet giriş

We are now accepting guest posting and creation of back-links.
This is default text for notification bar