• Post category:PHP

How to Remove Unwanted Space from PHP String

PHP Remove Space from String

To remove unwanted space from a string in PHP, we need to use the PHP trim() in-built function. This function removes leading and trailing unwanted whitespaces from a string. For example, see the below code.

Ex:

<!DOCTYPE html>
<html>
<body>

<?php
$str = " Hello Welcome";
echo trim($str);
?>

</body>
</html>

Output:

php remove space from string

Steps:

  1. Here, we have created a string variable “$str” with extra whitespaces.
  2. Then, I want to remove extra spaces so I use the PHP trim() function, and inside this function, we pass “$str”. It removes unwanted spaces from a string.
  3. See, the above output our extra spaces will remove successfully.
READ ALSO  Power Function in PHP |

Leave a Reply