Categories: PHP

trim – PHP String Functions

Syntax :

trim ( string, charlist);

Description :

trim() function will truncate spaces or other characters from the beginning and end of input string. It will remove “hi” from starting and “es.” from the end of input string “hi from tutorialmines.”.


Parameter :

  • string – This is a Required parameter. This is a string on which function will be performed.
  • charlist – This is a Optional parameter. The stripped characters can also be specified using the charlist parameter. You have to write down all characters which you want to be stripped. With .. you can specify a range of characters.

The following characters are removed if the charlist parameter is empty:

  • “\0” – NULL
  • “\t” – tab
  • “\n” – new line
  • “\r” – carriage return
  • ” ” –  ordinary white space
  • “\x0B” – vertical tab

Output :

This will return a trimmed string.


Related articles : rtrim(), ltrim().


trim() – PHP Functions Example 1 :
<?php
// below statement will trim white spaces from both sides of string."
$strExample = " Hi from tutorialmines. "; 
echo trim($strExample);
?>

In above example ,We have a string ” Hi from tutorialmines. “. Now this function will trim blank spaces from both side of string. Output of above code in the browser is as below:

Hi from tutorialmines.

trim() – PHP Functions Example 2 :
<?php
// below statement will trim white spaces from both sides of string."
$strExample = " \nHi from \ntutorialmines.\n\n ";
echo trim($strExample);
?>

In above example ,We have a string ‘ \nHi from \ntutorialmines.\n\n ‘. Now this function will trim blank spaces,\n character from both side of string. Output in ViewSource of browser is below ( HTML output of the code above):

<html><head></head><body>
Hi from 
tutorialmines.

 
</body></html>

See below is the output of above code.

Hi from tutorialmines.

trim() – PHP Functions Example 3 : Using .. you can specify a range of characters.
<?php
$strExample = "Hi from tutorialmines. This is a example of trim() function. Testing parameter"; 
echo trim($strExample,"Hi..parameter");
?>

In above example ,We have a string ‘Test Hi from tutorialmines. This is a example of trim() function. Testing parameter’. Now this function remove “Test ” from the starting and “parameter” from the end of string. See below is the output of above code.

from tutorialmines. This is a example of trim() function. Testing

Note :  trim() trims characters from the beginning and end of a string, it may be confusing when characters are (or are not) removed from the middle. trim(‘abc’, ‘bad’) removes both ‘a’ and ‘b’ because it trims ‘a’ thus moving ‘b’ to the beginning to also be trimmed. So, this is why it “works” whereas trim(‘abc’, ‘b’) seemingly does not.

jyoti rani

Recent Posts

Modern Toys, Magical Moments: Why the Best Toy Shop in Noida Is More Than Just a Store

When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…

6 months ago

Rediscovering Joy: A New Era of Creativity & Comfort in Toy Stores

In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…

6 months ago

Unboxing Imagination: Discovering the Joy of Play at a Toy Store in Noida

In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…

6 months ago

Imagination Unboxed: Discover Joy at the Toy Shop in Delhi

In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…

6 months ago

Play with Purpose: Discovering the Ultimate Toy Store in Noida

When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…

6 months ago

From Tears to Toys: Exploring Modern Childhood through Delhi’s Favorite Toy Shop

In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…

6 months ago