Challenge - 5 Problems
Substring Extraction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP substring extraction?
Consider the following PHP code. What will it output?
PHP
<?php $text = "Hello, world!"; echo substr($text, 7, 5); ?>
Attempts:
2 left
💡 Hint
Remember that substr starts at the offset and extracts the given length.
✗ Incorrect
substr($text, 7, 5) starts at index 7 (0-based) and extracts 5 characters: 'world'.
❓ Predict Output
intermediate2:00remaining
What substring does this code extract?
What will be the output of this PHP code snippet?
PHP
<?php $str = "Programming"; echo substr($str, -6, 3); ?>
Attempts:
2 left
💡 Hint
Negative start means counting from the end of the string.
✗ Incorrect
substr($str, -6, 3) starts 6 characters from the end and extracts 3 characters: 'amm'.
❓ Predict Output
advanced2:00remaining
What is the output when length exceeds string end?
What will this PHP code output?
PHP
<?php $s = "OpenAI"; echo substr($s, 3, 10); ?>
Attempts:
2 left
💡 Hint
If length goes beyond string end, substr returns until the end.
✗ Incorrect
substr($s, 3, 10) starts at index 3 and extracts till the end: 'nAI'.
❓ Predict Output
advanced2:00remaining
What happens if start is beyond string length?
What will this PHP code output?
PHP
<?php $str = "Data"; echo substr($str, 10, 2); ?>
Attempts:
2 left
💡 Hint
If start is beyond string length, substr returns empty string.
✗ Incorrect
substr($str, 10, 2) starts beyond string length, so returns empty string.
❓ Predict Output
expert3:00remaining
What is the output of this nested substring extraction?
Analyze the code and find the output:
PHP
<?php $text = "abcdefg"; $part = substr($text, 2, 4); echo substr($part, 1, 2); ?>
Attempts:
2 left
💡 Hint
First extract 4 chars from index 2, then extract 2 chars from index 1 of that result.
✗ Incorrect
substr($text, 2, 4) is 'cdef'. Then substr('cdef', 1, 2) is 'de'.