0
0
PHPprogramming~10 mins

Substring extraction 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 extract the first 5 characters from the string.

PHP
<?php
$text = "Hello, world!";
$part = substr($text, 0, [1]);
echo $part;
?>
Drag options to blanks, or click blank then click option'
A5
B7
C12
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong length value.
Confusing start position with length.
2fill in blank
medium

Complete the code to extract the substring starting from the 8th character to the end.

PHP
<?php
$text = "Hello, world!";
$part = substr($text, [1]);
echo $part;
?>
Drag options to blanks, or click blank then click option'
A7
B12
C0
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 6 instead of 7 as start position.
Forgetting that positions start at 0.
3fill in blank
hard

Fix the error in the code to correctly extract 5 characters starting from the 3rd character.

PHP
<?php
$text = "Programming";
$part = substr($text, [1], 5);
echo $part;
?>
Drag options to blanks, or click blank then click option'
A0
B2
C5
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3 as start position which is actually the 4th character.
Confusing character count with position index.
4fill in blank
hard

Fill both blanks to extract the last 4 characters of the string.

PHP
<?php
$text = "Substring";
$part = substr($text, [1], [2]);
echo $part;
?>
Drag options to blanks, or click blank then click option'
A-4
B4
C3
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using positive start position which extracts from the beginning.
Using wrong length value.
5fill in blank
hard

Fill both blanks to filter for word lengths greater than 3.

PHP
<?php
$words = ["apple", "cat", "banana", "dog"];
$lengths = array_filter(array_map(function($word) {
    return strlen($word);
}, $words), function($len) {
    return $len [1] [2];
});
print_r($lengths);
?>
Drag options to blanks, or click blank then click option'
A>
B3
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than or equal operators.
Confusing the comparison values.