PHP Program to remove white space form the string


May 1, 2022, Learn eTutorial
1624

What is meant by removing the white space from the string?

In this program, we are removing the white spaces from the string for that we are using the built-in function str_replace(). For example, if the text is "Welcome to PHP learning platform"  then the output will be "WelcometoPHPlearningplatform".

How to remove the white space from the string using PHP?

To remove the white space from the string we are using str_replace() and replacing the white space with nothing by doing this the white space will be removed from the string.

Syntax of str_replace()


str_replace(find,replace,string,count)

 

ALGORITHM

Step 1: Assign the string into the variable string

Step 2: Print the value of the variable string as the String before removing the white spaces

Step 3: Remove the white space by using the built-in function to replace str_replace()

Step 4: Print the value of the variable string as the String after removing the white spaces

PHP Source Code

                                          <?php
$string = "learne tutorials . com";
echo "String before removing the white spaces: $string \n";
$string = str_replace(' ', '', $string);
echo "String after removing the white spaces: $string";
?>
                                      

OUTPUT

String before removing the white spaces: learne tutorials . com
String after removing the white spaces: learnetutorials.com