Chose Language:
Author: Admin/Publisher |finished | checked

PHP trim function

Today we will see PHP trim which does this function where I can use it. This function removes white space from the beginning and end of a string by default.

Description / Descripción
trim(string $string, string $characters = " \n\r\t\v\x00"): string
Sintaxis / Sintax
$trimedString = trim($thestring);
$trimedString2 = trim($thestring, $characters);


$string – is the string passed as a parameter which will have the white spaces removed.

$characters – this parameter is optional, it allows you to provide the characters to be removed, its default value is ” \n\r\t\v\x00″, keep in mind that these characters to be removed are from the beginning and the end to be removed.


Returns the treated string, without white spaces at the beginning and at the end, depending on the $characters parameter, the specified characters will be removed or not.

PHP trim examples

Let’s make some examples of this function, what happens with the white spaces of the string that are in the middle of it?

// example of trim
$thestring = " hola mundo   ";
$trimedString2 = trim($thestring);

echo "".$trimedString2."";
hola mundo

We can se that whitespace in middle of string does not change, this is good and bad at the same time, good cause if you are writing text in an input it’s ok that whitespaces be there, bad cause sometimes we want to have no whitespace inside our input, one case is on user registry.

Ok, now suppose I want to remove the hyphens “-” but not the other characters from the string.

Eliminando otros caracteres
// example of trim
$thestring = "-hola mundo   -";
$charcter= "-";
$trimedString2 = trim($thestring,$charcter);

echo "".$trimedString2."";
hola mundo   

Using PHP trim

Remove white spaces: A user can sometimes press the space key, leaving a white space that we don’t want in our database.

Imagine that the user is entering their code and accidentally types a white space in the registry.

Removing white spaces saves us from having inconsistencies and problems with the data we enter.

Data Sanitization and Vulnerability Prevention: This point is taught in a PHP course, sanitize the data before processing it. What is not made so clear is the process of validating inputs.

Validating forms is one of the most important things when we are creating a website. It is the first filter from an attack on our site. Obviously, the trim function will not sanitize a form by itself, but it is part of the validation.

Improving user experience on the page: As we said before, a user may accidentally press the space key on the keyboard, trimming these errors can improve the user experience a bit.

Data comparison: Sometimes we may be comparing data with the data in our database, both the beginning and end white spaces can cause the comparison to fail and removing them will make the comparison more accurate.

Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category