0
0
PHPprogramming~10 mins

Trim functions in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to remove whitespace from the beginning and end of the string.

PHP
$text = "  Hello World  ";
$trimmed = [1]($text);
echo $trimmed;
Drag options to blanks, or click blank then click option'
Asubstr
Bstrlen
Cstrtoupper
Dtrim
Attempts:
3 left
💡 Hint
Common Mistakes
Using strlen instead of trim.
Using substr which does not remove spaces.
Using strtoupper which changes case but not spaces.
2fill in blank
medium

Complete the code to remove whitespace only from the beginning of the string.

PHP
$text = "  Hello World  ";
$trimmedStart = [1]($text);
echo $trimmedStart;
Drag options to blanks, or click blank then click option'
Atrim
Bltrim
Crtrim
Dstr_replace
Attempts:
3 left
💡 Hint
Common Mistakes
Using trim which removes spaces from both ends.
Using rtrim which removes spaces from the right end.
Using str_replace which replaces characters but does not trim.
3fill in blank
hard

Fix the error in the code to remove whitespace only from the end of the string.

PHP
$text = "  Hello World  ";
$trimmedEnd = [1]($text);
echo $trimmedEnd;
Drag options to blanks, or click blank then click option'
Atrim
Bltrim
Crtrim
Dsubstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using ltrim which removes spaces from the left.
Using trim which removes spaces from both ends.
Using substr which does not remove spaces.
4fill in blank
hard

Fill both blanks to create an array of trimmed words from the list, removing spaces from both ends.

PHP
$words = ["  apple  ", " banana", "cherry  "];
$trimmedWords = array_map([1], $words);
print_r($trimmedWords);
Drag options to blanks, or click blank then click option'
Atrim
Bltrim
Crtrim
Dstrtoupper
Attempts:
3 left
💡 Hint
Common Mistakes
Using ltrim or rtrim which only remove spaces from one side.
Using strtoupper which changes case but not spaces.
5fill in blank
hard

Fill all three blanks to trim spaces from both ends and check if the trimmed string is empty.

PHP
$text = "  ";
$trimmed = [1]($text);
if ($trimmed [2] '' ) {
    echo [3];
} else {
    echo 'Not empty';
}
Drag options to blanks, or click blank then click option'
Atrim
B==
C'Empty string'
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == in the condition.
Not trimming before comparison.
Printing wrong message.