Best way to strip all whitespace characters in a string in PHP

Many times, the strings that you are manipulating in a PHP project will have extra spaces that need to be removed. For instance, you might want to replace multiple spaces with a single space or you might want to get rid of all the whitespace in a string. Similarly, you might be planning on stripping all the whitespace from either the left or the right end of a string.

In this tutorial, you will learn about different techniques to do all that quickly and efficiently.

Strip or remove whitespace from the beginning of a String

If you want to remove whitespace only from the beginning of a string, you should use the ltrim() function in PHP. This function will remove the following whitespace characters: " " (an ordinary space), "\t" (a tab), "\n" (a new line), "\r" (a carriage return), "\0" (the NUL-byte) and "\x0B" (a vertical tab).

PHP

$sentence = "   This string contains a space and a tab in the beginning.";
$stripped = ltrim($sentence);

// Output — string(56) "This string contains a space and a tab in the beginning."
var_dump($stripped);

One good thing about ltrim() is that it allows you to specify some other problematic characters that you want to remove from the left end of any string. For instance, you could also use this function to remove . etc. in addition to whitespace. Another advantage of using ltrim() is that it is not very complicated to use and faster than using regular expressions.

Strip or remove whitespace from the end of a String

You can use the PHP rtrim() function to remove all the whitespace characters from the end of a string. This function works exactly like ltrim() but removes whitespace from the end. All the characters stripped by rtrim() are: " " an ordinary space, "\t" a tab, "\n" a new line, "\r" a carriage return, "\0" the NULL-byte and "\x0B" a vertical tab.

PHP

$sentence = "   This string contains a space and a tab at both ends.
";
// Output — string(57) "   This string contains a space and a tab at both ends.
"
var_dump($sentence);

$stripped = rtrim($sentence);
// Output — string(55) "   This string contains a space and a tab at both ends."
var_dump($stripped);

As you can see in the above example, the rtrim() function only removed the whitespace from the end. The whitespace in the beginning stays intact.

Strip or remove whitespace from both ends of a String

If you want to remove whitespace from both ends of a string, you should use the trim() function instead of using both ltrim() and rtrim(). Just like ltrim() and rtrim() this will remove the following characters: " " an ordinary space, "\t" a tab, "\n" a new line (line feed), "\r" a carriage return, "\0" the NUL-byte and "\x0B" a vertical tab.

PHP

$sentence = "   This string contains a space and a tab at both ends.     ";

$stripped = trim($sentence);
// Output — string(52) "This string contains a space and a tab at both ends."
var_dump($stripped);

Remove all the whitespace in a String

Some times, the strings you are working with will have unwanted whitespace in the middle and/or the beginning as well as the end. The trimming function we have discussed so far will be ineffective against it.

If you just want to remove all the whitespace characters irrespective of where they occur in the string, you should simply use str_replace() to replace all their occurrences with a blank string.

PHP

$sentence = "   This string contains a space and a tab at both ends.
";
$stripped = str_replace(' ', '', $sentence);

// Output — string(44) "Thisstringcontainsaspaceandatabatbothends.
"
var_dump($stripped);

It is important to remember that whitespace can consist of more than just space characters. In such cases, using the str_replace() function won’t cut it. You will have to use something more sophisticated like regular expressions. The special \s character in a regular expression is used to represent all whitespace characters that we removed by trim(), ltrim() and rtrim(). So, we will be using \s to represent all the whitespace that we want to go away.

PHP

$sentence = "   This string contains a space and a tab at both ends.
";
$stripped = preg_replace('/\s/', '', $sentence);

// Output — string(44) "Thisstringcontainsaspaceandatabatbothends."
var_dump($stripped);

Replace multiple whitespace characters with a single space

Most of the times when you decide to remove extra whitespace characters from a string, you would want to replace two or more of them with a single space character. This is different than removing all the spaces so we will have to make small changes in our regular expression and the preg_replace() function.

PHP

$sentence = "This   string
contains many whitespace characters
in                 it.";
$stripped = preg_replace('/\s+/', ' ', $sentence);

// Output — string(54) "This string contains many whitespace characters in it."
var_dump($stripped);

In the above example, the + after \s means that we want to replace one or more whitespace characters with a single space. The only problem now is that the main string might contain multiple whitespace characters at both ends. In such cases, there will be one space character present on both sides even after using preg_replace(). The solution here is to use trim() on the resulting string.

PHP

$sentence = "     This   string
contains many whitespace characters
in                 it.   ";
$stripped = trim(preg_replace('/\s+/', ' ', $sentence));

// Output — string(54) "This string contains many whitespace characters in it."
var_dump($stripped);

Quick summary

Let’s recap everything that we have covered in this tutorial.

  1. You can remove whitespace characters from the left and right end of a string using the ltrim() and rtrim() functions in PHP. The characters removed by these functions are : " " (an ordinary space), "\t" (a tab), "\n" (a new line), "\r" (a carriage return), "\0" (the NUL-byte) and "\x0B" (a vertical tab).
  2. You can also use the trim() function to remove whitespace characters from both ends at once. This will not have any effect on the whitespace inside the string.
  3. If you want to remove all the whitespace characters inside a string, you should use either str_replace() or preg_replace(). Both these functions will also be useful if you decide to replace multiple whitspace characters with a single space.

Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know other techniques to remove all the whitespace characters in a string using PHP.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top