0
0
PHPprogramming~10 mins

String search functions (strpos, strstr) 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 find the position of the word "world" in the string.

PHP
<?php
$text = "Hello world!";
$pos = [1]($text, "world");
echo $pos;
?>
Drag options to blanks, or click blank then click option'
Astrpos
Bstrstr
Csubstr
Dstrlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using strstr instead of strpos, which returns the substring instead of position.
Using substr which extracts part of the string.
Using strlen which returns string length.
2fill in blank
medium

Complete the code to get the part of the string starting from "world".

PHP
<?php
$text = "Hello world!";
$part = [1]($text, "world");
echo $part;
?>
Drag options to blanks, or click blank then click option'
Astrstr
Bstr_replace
Cstrpos
Dsubstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using strpos which returns position, not substring.
Using substr without position.
Using str_replace which replaces text.
3fill in blank
hard

Fix the error in the code to correctly find the position of "test" in the string.

PHP
<?php
$text = "This is a test string.";
$pos = [1]($text, "test");
echo $pos;
?>
Drag options to blanks, or click blank then click option'
Astr_replace
Bstrpos
Csubstr
Dstrstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using strstr instead of strpos.
Using substr which extracts substring by position.
Using str_replace which replaces text.
4fill in blank
hard

Fill both blanks to create a dictionary of words and their lengths for words longer than 3 characters.

PHP
<?php
$words = ["apple", "bat", "cat", "dog", "elephant"];
$lengths = array_filter(array_map(function($word) {
    return strlen($word);
}, $words));
$result = array_filter(array_combine($words, $lengths), function($length) {
    return $length [1] 3;
});
print_r($result);
?>
Drag options to blanks, or click blank then click option'
A==
B<
C>
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which filters shorter words.
Using '==' which filters words exactly length 3.
Using '!=' which filters words not equal to length 3.
5fill in blank
hard

Fill all three blanks to create an array of words starting with 'a' and their lengths.

PHP
<?php
$words = ["apple", "banana", "apricot", "cherry", "avocado"];
$filtered = array_filter($words, function($word) {
    return [1]($word, 'a') === 0;
});
$lengths = array_map(function($word) {
    return [2]($word);
}, $filtered);
$result = array_combine($filtered, $lengths);
print_r($result);
?>
Drag options to blanks, or click blank then click option'
Astrpos
Bstrlen
Cstrstr
Dsubstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using strstr which returns substring, not position.
Using substr which extracts substring by position.
Using wrong function for length.