- 1). Open your PHP file in a text editor, such as Windows Notepad.
- 2). Store a string value in a PHP variable. For example, "$my_string = " hello world ";".
- 3). Call the "trim(string, ' ')" function to remove leading and trailing spaces from the string. For example, "$my_string = trim($my_string, ' ');" will return "hello world."
- 4). Call "ltrim(string, ' ')" or "rtrim(string, ' ')" if you only want to remove spaces from the beginning or the end. The function "ltrim" will only remove matching characters from the beginning of the string, while "rtrim" will only remove matching characters from the end of the string. For example, "$my_string = ltrim($my_string, ' ');" will return "hello world ";".
- 5). Save the PHP file and load it on your server. If it doesn't work correctly, re-check the code syntax for any typos or errors.
SHARE