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.
<!DOCTYPE html> <html> <body> <?php $str = " Hello Welcome"; echo trim($str); ?> </body> </html>
- Here, we have created a string variable “$str” with extra whitespaces.
- 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.
- See, the above output our extra spaces will remove successfully.