Removing Spaces in PHP: Performance and Pitfalls

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Remove All Spaces from a String in PHP: From Beginner to Pro

\n\n

Imagine this: you receive a phone number from a user, and they entered it with a bunch of spaces, tabs, and line breaks. Or you are parsing a CSV file where values are wrapped in extra spaces. Sound familiar? The question "How do I strip all spaces out of a string in PHP?" on Stack Overflow has gathered tens of thousands of views — and for good reason. Every PHP developer eventually faces the task of cleaning a string of whitespace characters.

\n\n

Today, we won't just give you the answer. We'll conduct a real investigation: break down all the popular methods, compare their performance, and figure out which one you should use in real projects. Get your IDEs ready — it's going to be interesting.

\n\n

Why You Shouldn't Use trim()

\n\n

Let's start with a common mistake. Many beginners write:

\n\n
$clean = trim($dirty);
\n\n

And think the problem is solved. But trim() only removes spaces from the beginning and end of a string. If there are spaces inside, they will remain. It's like sweeping only the doorstep, leaving all the dirt in the middle of the room.

\n\n

For our task, we need more powerful tools. Let's break down three main approaches to removing all spaces from a string in PHP.

\n\n

Method 1: str_replace — The King of Simplicity

\n\n

The most obvious and frequently used method is the str_replace() function. It replaces all occurrences of a substring with an empty string. Simple as two plus two:

\n\n
<?php\n$string = "Hello, world! How are you?";\n$no_spaces = str_replace(' ', '', $string);\necho $no_spaces; // "Hello,world!Howareyou?"\n?>
\n\n

However, there is a nuance here. str_replace() only removes regular spaces (character code 32). What about tabs, non-breaking spaces, or line breaks? For a complete cleanup, you'll need to pass an array:

\n\n
<?php\n$string = "Hello\\tworld!\\nHow are you?";\n$remove = [' ', "\\t", "\\n", "\\r"];\n$no_spaces = str_replace($remove, '', $string);\necho $no_spaces; // "Helloworld!Howareyou?"\n?>
\n\n

Pros: high speed, code readability.
\nCons: you need to explicitly list all types of spaces.

\n\n

What About str_ireplace?

\n\n

Some developers confuse str_replace with str_ireplace. The second function does the same thing, but is case-insensitive. For spaces, there is no difference — a space has no case. So feel free to use str_replace.

\n\n

Method 2: preg_replace — The Power of Regular Expressions

\n\n

When you need to remove all whitespace characters at once, regular expressions come to the rescue. The character class \\s includes space, tab, line feed, carriage return, and even vertical tab:

\n\n
<?php\n$string = "Hello\\tworld!\\nHow are you?";\n$no_spaces = preg_replace('/\\s+/', '', $string);\necho $no_spaces; // "Helloworld!Howareyou?"\n?>
\n\n

Note the plus sign (+) after \\s. It means "one or more whitespace characters". If you write just /\\s/, each whitespace character will be replaced individually — this also works, but is slightly slower.

\n\n

Pros: versatility, covers all types of spaces.
\nCons: about 2-3 times slower than str_replace on large data volumes.

\n\n

When to Choose preg_replace

\n\n

Use this method if:

\n
    \n
  • You don't know exactly which whitespace characters might appear
  • \n
  • You need to remove all "invisible" characters, including rare ones (e.g., Unicode non-breaking space)
  • \n
  • You are processing strings from external sources (API, HTML parsing)
  • \n
\n\n

Method 3: strtr — The Dark Horse

\n\n

Few people know, but PHP has the strtr() function (

Blogs

Book Recommendations