How to Remove Whitespace from a String in PHP

php


In this article, we show how to remove whitespace either from the left side of a string or the right side of a string using the PHP trim() function.

The trim() function erases white space from the left and right side of a string.

This will be demonstrated below.

So, if we have the string "GPS ", after passing it through the trim() function, the string would then change to "GPS".

The trim() function erases white space from the left and right sides of a string. So if the string is " GPS", and we apply the trim() function, it would change to "GPS". Similarly, the string, " GPS ", would change to "GPS".

Example

Below is an example of the trim() function in action, which removes all whitespace from the left and right side of a string





Actual PHP Output

GPS
GPS
GPS



The trim() function is a very useful and powerful PHP function. An example of prime use of the trim() function is a search box, where a user enters in a search term. For example, say if a user is looking up hotels in Hawaii, he or she may add a space at the beginning or end of the search query by mistake. In PHP, " hotels" is not equal to "hotels". To prove this, let's run the following PHP code.



The output of the above PHP code is:

These two are not recognized as the same

So you can see that PHP does not recognize the 2 strings as being equal. "hotels" is not the same as " hotels". Neither is "hotels  " the same as "hotels". However, if a user enters in these search queries, we know that the search queries mean the same thing. To make them the same, we use built-in PHP trim() function. This erases all white space before the beginning of a string and all white space after the end of a string. After we use it, then all the search queries will be the same, despite the white space.

To prove this, run the following PHP code:



The output of the above PHP code is:

Before the trim() function
These 2 strings are not the same
These 2 strings are also not the same

After the trim() function
These 2 strings are the same
These 2 strings are also the same


So this is the perfect example to illustrate the power of the PHP trim() function to equalize strings that vary only in the white space before or after them.

How to Remove All Spaces in a String

So the PHP trim() function we demonstrated above removes whitespace from the front or end of a string.

If we want to remove all spaces in a string with PHP, the trim() function would not be used. Instead, we could use the str_replace() function in the following way.



The str_replace() takes in 3 parameters. The first parameter is the character(s) you want to replace. The second parameter is what you want to replace it with. And the third parameter is the string that you want to change, the object of the replacement.

Since we want to replace whitespace with none, the first parameter is whitespace (" "). The second parameter is to replace the whitespace with none (no whitespace). The third parameter is the string that we want to manipulation with these replacements.

This will remove all whitespace from a string.

Actual PHP Output

WhatdayoftheweekisJuly4ththisyear?

Sometimes this may be the case where you want to remove all whitespace from a string. Usually this is not the intention. The trim() function is more popular, especially when considering search queries. Normally whitespace just needs to be removed from the beginning or end of a string.

However, we demonstrate how to do both just in case you need to use both approaches.


HTML Comment Box is loading comments...