0
0
PHPprogramming~15 mins

Trim functions in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Trim functions
📖 Scenario: Imagine you are working on a website where users enter their names and comments. Sometimes, users accidentally add extra spaces before or after their input. You want to clean up these inputs to store neat data.
🎯 Goal: You will create a PHP script that uses trim functions to remove unwanted spaces from user input strings.
📋 What You'll Learn
Create a variable with a string that has extra spaces at the start and end
Create a variable to hold the characters to trim
Use PHP trim functions to clean the string
Print the cleaned strings
💡 Why This Matters
🌍 Real World
Cleaning user input is important to store consistent data and avoid errors caused by extra spaces.
💼 Career
Many jobs require handling user data carefully, and knowing how to trim strings is a basic but essential skill.
Progress0 / 4 steps
1
Create a string with extra spaces
Create a variable called $userInput and set it to the string " Hello World! " including the spaces at the start and end.
PHP
Need a hint?

Remember to include the spaces inside the quotes.

2
Create a variable with characters to trim
Create a variable called $charsToTrim and set it to the string " H!" which includes a space, capital H, and exclamation mark.
PHP
Need a hint?

This string tells PHP which characters to remove from the start and end.

3
Use trim functions to clean the string
Create three variables: $trimmed using trim($userInput), $trimmedChars using trim($userInput, $charsToTrim), and $leftTrimmed using ltrim($userInput).
PHP
Need a hint?

Use the PHP trim functions exactly as shown.

4
Print the cleaned strings
Print the variables $trimmed, $trimmedChars, and $leftTrimmed each on a new line using echo and "\n" for line breaks.
PHP
Need a hint?

Use echo to print each variable followed by a newline character.