How to replace spaces in a string using PHP

You can replace spaces in different ways. Not all spaces showing in browser’s output are same. They could vary in type. Because some of them could include tabs and line ends, or could include &nbsp in HTML. So you need to identify them first.

But you can handle almost all of them using str_replace() and preg_replace() function.

For normal spaces, use str_replace()

Syntax

str_replace('substring', 'replaced_string', 'original_string');

Or

preg_replace('substring', 'replaced_string', 'original_string');

We need to provide three parameters.

1. Substring is the first parameter which should be replaced with replaced_string.

2. Replaced_string will replace the substring.

3. Original_string will contain the full string from where you want to replace spaces.

In the placement of parameters, you can use PHP variables.

Discussion: Replace spaces in a string

Here I putted different examples. Pick the best solution for you.

Example 1: Replace normal spaces

This code will replace all spaces with empty string.

<?php
//input string
$string = 'Welcome to TorikulIslam.com';            
//strip simple spaces
$string = str_replace(' ', '', $string);
//showing result
echo $string;
?>

Output:

WelcometoTorikulIslam.com

If you want to replace spaces with - , then follow the code below

Example 2: Replace all spaces with hyphen

This code will replace all spaces with hyphen (-).

<?php
//input string
$string = 'Welcome to TorikulIslam.com';            
//replace spaces with hyphen
$string = str_replace(' ', '-', $string);
//showing result
echo $string;
?>

Output:

Welcome-to-TorikulIslam.com

Note: However, the above examples will only replace spaces. If you want to remove all whitespaces including tabs, newlines, etc, you can use the preg_replace() function. This function perform a regular expression search and replace.

Example 3: Remove tab and newline spaces

This code will replace all whitespaces.

<?php
//input string
$string = "This is a newline space \nexample, and this is a tab space \texample.";    
//replace spaces with hyphen
$string = preg_replace('/\s+/', '_', $string);
//showing result
echo $string;
?>

Output:

This-is-a-newline-space-example,-and-this-is-a-tab-space-example.

If you run this string through str_replace() function, it will show you the output below:

This-is-a-newline-space- example,-and-this-is-a-tab-space- example.

Here is two spaces still exist before the word Example.

Note: In the above example \t represents the tab character, whereas \n represents the newline character.

Even this solution won’t replace the spaces arise from HTML special character. To solve this problem follow the code below.

Example 4: Replace spaces of HTML special character

This code will replace all HTML special character spaces.

<?php
//input string
$string = "This is a simple example.";    
//replace HTML special characters
$string = str_replace(' ', '-', $string);
//showing result
echo $string;
?>

Output:

This-is-a-simple-example.