php tutorial - PHP trim function for removing spaces - php programming - learn php - php code - php script



Purpose of trim function in PHP

The PHP trim function is used to remove spaces or other characters like a tab, new line, a vertical tab etc. in a given string.

Sometimes, it is required to ensure that the text entered from the users does not contain leading or trailing whitespaces before storing into the database or displaying 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" ] )
click below button to copy the code. php tutorial - team

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.

 php-trim

Learn php - php tutorial - php-trim - php examples - php programs

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 clearly.

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>'";
click below button to copy the code. php tutorial - team

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), new line 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:

 php-trim-tabs

Learn php - php tutorial - php-trim-tabs - php examples - php programs

This is how the tabs are added in a string and trim function is used:

$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>'";
click below button to copy the code. php tutorial - team

Note that, if you specify”\t” as character mask, it will also strip the tabs:

$trimmed_string = trim($source_string, "\t");
click below button to copy the code. php tutorial - team

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:

 php-ltrim

Learn php - php tutorial - php-ltrim - php examples - php programs

You can see, the output string after using the ltrim function has removed the space from left only. The space at 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-rtrim

Learn php - php tutorial - php-rtrim - php examples - php programs

By using rtrim function:

$trimmed_string = rtrim($source_string);
click below button to copy the code. php tutorial - team

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 above examples, the span tag is used to highlight element values so that spaces are clearly visible.
 php-trim-arrays

Learn php - php tutorial - php-trim-arrays - php examples - php programs

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

function trim_array_value(&$value)
{
$value = trim($value);
}
click below button to copy the code. php tutorial - team

This code creates that array and calls the function where the foreach loop is used twice:

$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 />";
}
click below button to copy the code. php tutorial - team


Related Searches to PHP trim function for removing spaces