How to remove white spaces from a string using PHP, php trim string,  remove spaces in php, rtrim in php, trim in php, ltrim in php, str_replace in php

How to remove white spaces from a string using PHP

In PHP language or any other language, the white spaces may change the meaning or definition of a string since white spaces are counted as characters.

If you do not get rid of white spaces where there are not needed you will likely get the result which you did not intend to produce. Therefore, there is a need to always remove white spaces in a string when they are not needed.

For example, in a scenario where you are allowing users to upload images to the server and then storing the image name in a database and you do not remove the whitespaces, you will get outputs that are not functioning since the value stored in the database will have spaces meaning that the browser will not recognize it as a complete URL

There are different ways or functions supported by PHP of removing white spaces from a string depending on the result you want to get, that is, you can check for white spaces before and at the end of the string and also check for white spaces in between words.

In this article, we shall focus on different functions provided by PHP that are used to remove white spaces from a string

  • Removing a white space at the beginning and end of a string

The white spaces that occur at the beginning and at the end of a string are mostly not intentional meaning they occur by mistake.

An example of a white space that is at the beginning and the end of a string is as follows

<?php

$string1=" Hello, this is a test whitespace ";

?>

The above string has white spaces at the beginning and at the end of the string.

The above string is not the same as this string below

<?php

$string2="Hello, this is a test whitespace";

?>

To determine that the two strings $string1 and $string2 are different, we calculate the length of each string as below

<?php

$string1=" Hello, this is a test whitespace ";

$string2="Hello, this is a test whitespace";

//calculating the length of strings

echo $string1_length=strlen($string1);

echo $string2_length=strlen($string2);

?>

The length of string 1 will be 34 while the length of string 2 will be 32 meaning that the strings have different lengths and therefore different meanings

To remove the white spaces at the beginning and at the end of the string, we use the trim() function

The trim() function removes any white space before and at the end of the string

<?php

$string1=" Hello, this is a test whitespace ";

// removing the white space using the trim function

echo trim($string1);

// the result of the string after using the trim function will be

"Hello, this is a test whitespace";

?>

Remove trailing / at the end of a URL

Another example where trim() function is used is to remove the trailing / at the end of a URL

<?php

// url with a trailing / at the end

$uri = "https://www.solutionspacenet.com/";

 

// use the trim function to remove the trailing / from a URL

echo trim($uri,'/');

// the result after using the trim function will be as follows

https://www.solutionspacenet.com

?>

Remove white spaces at the left of the string

There is also an option to remove only the white spaces that are at the beginning of a string without interfering with the ones at the right.

PHP provides the use of ltrim() function which removes the white spaces that are at the beginning or left of a string

<?php

$string1=" Hello, this is a test whitespace ";

// removing the white space at the beginning of a string using the ltrim function

echo ltrim($string1);

// the result of the string after using the ltrim function will be

"Hello, this is a test whitespace ";

?>

Remove white spaces at the right or end of a string

You can also do away with the white spaces that are at the end of a string only by using the rtrim function in PHP which ensures that the white spaces at the end or right of the string are trimmed or removed

<?php

$string1=" Hello, this is a test whitespace ";

// removing the white space at the end of a string using the rtrim function

echo rtrim($string1);

// the result of the string after using the rtrim function will be

" Hello, this is a test whitespace";

?>

Removing all white spaces from a string

In a case where you want to remove all white spaces from a string, that is, if you have a string that has 5 words, you remove all the white spaces and be left with a string that is only a single word.

This is mostly practicable when you either want to generate a single word that can be used to rename a name of an image after it has been uploaded or when you want to replace the white spaces with a different character

To remove white spaces from a string in PHP, we use the str_replace() function which you must define the characters or white space you want to remove and how you want to replace it.

The str_replace function must have what is being removed and what is replacing it.

The syntax for str_replace() is as follows

str_replace(what_to_remove, what_to_replace_with, the_string)

An example of using the str_replace function in PHP is as follows

<?php

$string1=" Hello, this is a test whitespace ";

// replacing the white spaces with no space using str_replace

echo str_replace(" ","",$string1);

// the result of the string after using the str_replace function will be

Hello,thisisatestwhitespace

?>

You can also use the str_replace function to add other character in between the words, mostly you can create a URL slug as follows using the hyphen (-) or underscore (_) as follows

<?php

$string1="Hello this is a test whitespace ";

// replacing the white spaces with aa hypen (-) using str_replace

echo str_replace(" ","-",$string1);

// the result of the string after using the str_replace function will be

Hello-this-is-a-test-whitespace

?>

Hope you have learned how to remove white spaces from a string in PHP using trim, ltrim, rtrim and str_replace functions. That’s all for now