How to Remove Space from PHP String

PHP

With the help of trim function we can remove space from the string .
Trim is the PHP Function which is used to Remove the White Spaces from the String and make the content perfect for use.
The trim() function removes whitespaces and other predefined characters from both sides of a string.

Related functions:

ltrim() – Removes whitespace or other predefined characters from the left side of a string
rtrim() – Removes whitespace or other predefined characters from the right side of a string

Example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>How to Remove Space from PHP String </title>
</head>
<body>
<?php
$test = '  Hello Hai  ';
echo $test;
$test = trim($test);
echo $test;
It will print 'Hello Hai' without white spaces.
Same way you can use ltrim() and rtrim().

ltrim($test)
Output 'Hello Hai  '
//This will Remove Space from Left Side

rtrim($test)
Output '  Hello Hai'
//This will remove Space from Right Side
?>
</body>
</html>
-->