Complete the code to find the position of the word "world" in the string.
<?php $text = "Hello world!"; $pos = [1]($text, "world"); echo $pos; ?>
The strpos function returns the position of the first occurrence of a substring in a string.
Complete the code to get the part of the string starting from "world".
<?php $text = "Hello world!"; $part = [1]($text, "world"); echo $part; ?>
The strstr function returns the part of the string starting from the first occurrence of the substring.
Fix the error in the code to correctly find the position of "test" in the string.
<?php $text = "This is a test string."; $pos = [1]($text, "test"); echo $pos; ?>
To find the position of a substring, use strpos. strstr returns the substring, not the position.
Fill both blanks to create a dictionary of words and their lengths for words longer than 3 characters.
<?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); ?>
The code filters words with length greater than 3 using the '>' operator.
Fill all three blanks to create an array of words starting with 'a' and their lengths.
<?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); ?>
The code uses strpos to check if the word starts with 'a' (position 0), and strlen to get word lengths.