How to Use the Trim Function

104 17
    • 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

Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.