PHP Trim Function

Illustration of scissors trimming a string: Understanding PHP Trim Function

Purpose of trim function in PHP

  • The PHP trim function is used to remove spaces or other characters like a tab, a new line, a vertical tab, etc. in a given string.
  • Sometimes, it is required to ensure that the text entered by the users does not contain leading or trailing whitespaces before storing it in the database or displaying it in an HTML element.
  • The trim PHP function can be easily used to accomplish that.

Syntax of trim function

This is how you may use the trim function in PHP programs:

$trimmed_string = trim ($string_to_be_trimmed [, string $character_mask = ” \t\n\r\0\x0B” ] )
The trim function returns the trimmed string. If you do not specify any characters in the second parameter, the trim function not only removes the single space but tabs, new lines, etc.

See the following examples of using the trim function for removing spaces and other characters in the given string.

An example of using trim function

In this example, a string is created with leading and trailing spaces. After that, the trim function is used to remove those spaces.

The string is displayed before and after using the trim function.

An HTML span tag with the background color is used for displaying the strings before and after using the trim function to see the difference.

The following PHP code is executed:

$source_string = ("  This is a trim function tutorial ");

$trimmed_string = trim($source_string," ");



echo "The source string: <BR>'<span>" .$source_string ."</span>'<BR><BR><BR>";

echo "The trimmed string: <BR>'<span>" .$trimmed_string ."</span>'";

Complete code (HTML and PHP)

<!doctype html>
<html>

<head>

<style>
.divcls {
  background: #004000;
  height: auto;
  width:290px;
  border-radius: 15px;
  padding:20px;
  font-size:17px;
  color:#fff;   
}
span{
    background: #FA4238;
}
</style>
 

  
</head>
<body>
<div class="divcls"><h3>Demo of PHP trim function</h3><br /><br />
< ?php
$source_string = ("  This is a trim function tutorial ");


$trimmed_string = trim($source_string," ");

echo "The source string: <BR>'<span>" .$source_string ."</span>'<BR><BR><BR>";
echo "The trimmed string: <BR>'<span>" .$trimmed_string ."</span>'";
?>
</div>
</body>
</html>

An example of using tab (\t) in a string

As mentioned earlier, if you do not specify the character mask in the trim function, it will not only remove the single space but if tab (\t), a newline character (\n), a carriage return (\r), etc. are used in a string, they will also be removed while using the trim function.

In this example, leading and trailing spaces are added by using the \t character. After that, the trim function is used without specifying the character mask:

<!doctype html>
<html>

<head>

<style>
.divcls {
  background: #400000;
  height: auto;
  width:290px;
  border-radius: 15px;
  padding:20px;
  font-size:17px;
  color:#fff;   
}
span{
    background: #FFFF00;
    color:#000
}
</style>
 

  
</head>
<body>
<div class="divcls"><h3>Demo of PHP trim function</h3><br /><br />
< ?php
$source_string = "\tThis is a trim function tutorial\t";


$trimmed_string = trim($source_string);

echo "The source string: <BR>'<span>" .$source_string ."</span>'<BR><BR><BR>";
echo "The trimmed string: <BR>'<span>" .$trimmed_string ."</span>'";
?>
</div>
</body>
</html>

Output:

PHP trim tabs

Note that, if you specify”\t” as a character mask, it will also strip the tabs:
$trimmed_string = trim($source_string, “\t”);

An example of removing left space only

If you require removing white spaces from the left of a string then you may use the PHP ltrim function.

In this example, the source string contains space on both sides.

The ltrim function is used to remove the space from the left side of the string only. See the code and output:

<!doctype html>
<html>

<head>

<style>
.divcls {
  background: #400000;
  height: auto;
  width:290px;
  border-radius: 15px;
  padding:20px;
  font-size:17px;
  color:#fff;   
}
span{
    background: #FFFF00;
    color:#000
}
</style>
 

  
</head>
<body>
<div class="divcls"><h3>Demo of PHP trim function</h3><br /><br />
< ?php
$source_string = " Using the ltrim function to remove space from left ";


$trimmed_string = ltrim($source_string);

echo "The source string: <BR>'<span>" .$source_string ."</span>'<BR><BR><BR>";
echo "Outut of ltrim function: <BR>'<span>" .$trimmed_string ."</span>'";
?>
</div>
</body>
</html>

Output:

PHP ltrim

You can see that the output string after using the ltrim function has removed the space from left only. The space on the right side is still visible.

Removing white space from the right side example

Similarly, PHP has the rtrim function for removing the white spaces from the right side of the string.

A demo is shown in this example:

PHP code and markup:

<!doctype html>
<html>

<head>

<style>
.divcls {
  background: #0000A0;
  height: auto;
  width:290px;
  border-radius: 15px;
  padding:20px;
  font-size:17px;
  color:#fff;   
}
span{
    background: #FFFF00;
    color:#000
}
</style>
 

  
</head>
<body>
<div class="divcls"><h3>Demo of PHP trim function</h3><br /><br />
< ?php
$source_string = " Using the ltrim function to remove space from right ";


$trimmed_string = rtrim($source_string);

echo "The source string: <BR>'<span>" .$source_string ."</span>'<BR><BR><BR>";
echo "Outut of rtrim function: <BR>'<span>" .$trimmed_string ."</span>'";
?>
</div>
</body>
</html>

Output:

PHP rtrim

By using rtrim function:

$trimmed_string = rtrim($source_string);

The space from the right side is striped while it is still visible in the left side of the trimmed string.

An example of trim function with PHP arrays

You may delete white spaces in PHP arrays as well by using the trim function.

  • In this example, an array of three elements is created with leading and trailing spaces in each element.
  • A PHP function is created where the trim function is used to delete spaces from both sides for each element.
  • The array elements are displayed before and after using the trim function so you can see the difference.
  • Just like the above examples, the span tag is used to highlight element values so that spaces are visible.

Code:

<!doctype html>
<html>

<head>

<style>
.divcls {
  background: #0000A0;
  height: auto;
  width:290px;
  border-radius: 15px;
  padding:20px;
  font-size:17px;
  color:#fff;   
}
span{
    background: #FFFF00;
    color:#000
}
</style>
 

  
</head>
<body>
<div class="divcls"><h3>Demo of PHP trim function</h3><br /><br />
< ?php

function trim_array_value(&$value) 
{ 
    $value = trim($value); 
}

$arr_courses = array(" PHP ", " HTML ", " MYSQL ");
echo "Array before trim: <BR><BR>";
foreach( $arr_courses as $arritem )
{
echo "The array item is:<span>" .$arritem ."</span><br />";
}

array_walk($arr_courses, 'trim_array_value');


echo "<BR><BR>Array after trim: <BR><BR>";
foreach( $arr_courses as $arritem )
{
echo "The array item is:<span>" .$arritem ."</span><br />";
}
?>
</div>
</body>
</html>

Output:

PHP trim arrays

This is how a PHP function is created and trim function is used:

function trim_array_value(&$value)

{

$value = trim($value);

}

You can see the difference before and after using the trim function.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!