PHP Basics

PHP: Remove Extra Spaces around a string

In many times, you may see there are strings with extra spaces around it and you may want to remove those extra spaces. By using PHP function trim function, you can remove those extra spaces around the string.



echo $username;


Output: ”    srikant                                                        “

Notice here, the variable $username contains the string value srikant and at the beginning and at the end there are unwanted white spaces. By using PHP function trim() we can remove these white spaces.

PHP Trim() Function

To remove the extra spaces around the value srikant, I can use trim function.







echo trim($username);



PHP Trim function removed the white space at the beginning of the string and at the end of the string.