Complete the code to extract the first 5 characters from the string.
<?php $text = "Hello, world!"; $part = substr($text, 0, [1]); echo $part; ?>
The substr function extracts a part of the string starting at position 0 for 5 characters.
Complete the code to extract the substring starting from the 8th character to the end.
<?php $text = "Hello, world!"; $part = substr($text, [1]); echo $part; ?>
Starting at position 7 extracts the substring from the 8th character to the end.
Fix the error in the code to correctly extract 5 characters starting from the 3rd character.
<?php $text = "Programming"; $part = substr($text, [1], 5); echo $part; ?>
Position 2 is the 3rd character because counting starts at 0.
Fill both blanks to extract the last 4 characters of the string.
<?php $text = "Substring"; $part = substr($text, [1], [2]); echo $part; ?>
Using a negative start position counts from the end. Length 4 extracts the last 4 characters.
Fill both blanks to filter for word lengths greater than 3.
<?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); ?>
The filter keeps lengths greater than 3.
