How to Use the Trim Function
Text processing is especially important in Web development programming languages such as PHP. PHP includes a special "Trim" function that removes all white spaces before or after a word. This allows a word to fit more appropriately inside the HTML page it's designed for. Using the "Trim" function is easy and accessible to people with little programming experience. By using trim, words fit nicely in the text boxes provided for them and don't leave huge, empty spaces on Web pages.
Instructions
-
-
1
Decide how you want to run your PHP code. If you have a PHP server, you can execute code using PHP files. If you don't have access to a PHP server, you can use an online PHP interpreter. Enter the code in this tutorial into either a PHP file or the online PHP interpreter.
-
2
Begin your PHP program with the following statement:
<?php
-
-
3
Declare a string variable and assign it some text value. This text must contain leading and trailing extraneous spaces. For example, you can write the following:
$text = " extra spaces ";
-
4
Trim the text by passing the "$text" variable to the "trim" function. Save the resulting trimmed string into another variable, like this:
$trimmedText = trim($text);
-
5
Print the values held by each string using the following three print statements; the middle statement just prints a blank line between outputs:
print($text);
print("\n");
print($trimmedText);
-
6
Conclude your PHP program with the statement below. Your program is now ready to be tested on your PHP server or online PHP interpreter.
?>
-
7
Execute the program. The output should look like this:
extra spaces
extra spaces
-
1