PHP

How To Remove Spaces From String Using PHP

When dealing with strings, a single space or multiple spaces could be the part of content. So at times, it might be necessary to remove this single space or remove the multiple spaces from string. So if you do not know how to remove space from string, then this article is for you. In this article, I am going to share a very easy way to use PHP to remove single space or multiple spaces from string by means of examples. So read on to find out more.

Examples on using PHP to Remove Spaces From String

To better illustrate how to remove space from string using PHP, let’s assume that we have some sample content and this content consists of spaces. Now we would like to remove the spaces in a single shot using PHP. So you can use the code below.

Example: Using PHP to remove spaces from string

<?php

$content = 'Hello, How are you doing today?';

echo 'Original Content:'; echo '<br />';
echo $content;
echo '<br /><br />';

//Remove all spaces from string using the function below
$new_content = str_replace(' ', '', $content);

echo 'New Content:'; echo '<br />';
echo $new_content;
echo '<br />';

?>

Simple, isn’t it?

Do you know of any other ways to use PHP to remove spaces from string? Feel free to suggest by commenting below.

Share your thoughts, comment below now!

*

*