Signup/Sign In

How to remove white space from a string in PHP?

Answer: Using trim() function

We can remove the whitespace from the string using the trim() function. Along with the white space, this function removes the special characters present at the starting and end of the string.

The trim() function removes the white spaces, including non-breaking spaces, newlines, tabs, etc., from the beginning and end of the string.

Example: Removing whitespace using trim() function

In the given example, we have removed the whitespace from the given string using the trim() function.

<!DOCTYPE html>  
<html> 
<head>
	<title>Removing white space from a string</title>
</head> 
<body>  
	<?php
		$str = '      Welcome to Studytonight '; 
		$newStr = trim($str); 
		echo $newStr. "<br>";
		echo "String length of input string is: " . strlen($str) . "<br>";
		echo "String length of the output string is: ". strlen($newStr);
	?>
</body>  
</html>  


Welcome to Studytonight
String length of input string is: 30
String length of the output string is: 23

Using ltrim() function

We can also remove the whitespace using the ltrim() function. Previously we have discussed the trim() function.

The trim() function removes the white space from both sides (beginning and end) of the string. But what if we want to remove the whitespace only from the left side of the string. We can remove the whitespace and special characters from the left of the string using the ltrim() function.

Example: Removing whitespace using ltrim() function

In the given example, we have removed the white space using ltrim() function.

<!DOCTYPE html>  
<html> 
<head>
	<title>Removing white space from a string</title>
</head> 
<body>  
	<?php
		$str = '      Welcome to Studytonight '; 
		$newStr = ltrim($str); 
		echo $newStr. "<br>";
		echo "String length of input string is: " . strlen($str) . "<br>";
		echo "String length of the output string is: ". strlen($newStr);
	?>
</body>  
</html>  


Welcome to Studytonight
String length of input string is: 30
String length of the output string is: 24

As we can see in the output, the length of the output string is 24 while in the previous example length is 23. This is because the trim() function removes the whitespace from both sides while the ltrim() function removes only from the left of the string.

Using rtrim() function

In the previous example, we have learned how to remove the whitespace from the left side of the string only. Now we are going to learn how to remove the white space from the right side of the string.

We can remove the whitespace from the right side of the string using the rtrim() function. This function takes a string as its input value and returns a string having no extra whitespace to the right of the string.

Example: Removing whitespace using rtrim() function

In the given example, we have removed the whitespace using the rtrim() function.

<!DOCTYPE html>  
<html> 
<head>
	<title>Removing white space from a string</title>
</head> 
<body>  
	<?php
		$str = '      Welcome to Studytonight '; 
		$newStr = rtrim($str); 
		echo $newStr. "<br>";
		echo "String length of input string is: " . strlen($str) . "<br>";
		echo "String length of the output string is: ". strlen($newStr);
	?>
</body>  
</html>  


Welcome to Studytonight
String length of input string is: 30
String length of the output string is: 29

The difference between the length of the input string and the output string is 1. This is because there is only one extra white space at the right of the string.

Conclusion

In this lesson, we have learned how o remove white space from the string in PHP. So here, we have discussed three methods to remove whitespace from the given string. At first, we have used the trim() function. This function removes the white space from both sides of an array. Then we removed the white space using the ltrim() function. This function removes the white space only from the left of the string. In the last, we have removed white space using the rtrim() function. This function removes whitespace from the right of the string.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.