📖 Scenario: Imagine you have a list of book titles, and you want to extract a short preview from each title to show on a website.
🎯 Goal: You will create a PHP program that extracts a substring (a part of a string) from each book title to create a preview snippet.
📋 What You'll Learn
Create an array of book titles
Set a variable for the length of the preview substring
Use a loop to extract the substring from each title
Print the preview snippets
💡 Why This Matters
🌍 Real World
Extracting substrings is useful for showing previews or summaries of longer text, like book titles, article headlines, or product names on websites.
💼 Career
Many programming jobs require manipulating text data, and substring extraction is a common task in web development, data processing, and user interface design.
Progress0 / 4 steps
1
Create an array of book titles
Create an array called books with these exact titles: 'The Great Gatsby', 'To Kill a Mockingbird', '1984', 'Pride and Prejudice', 'Moby-Dick'.
PHP
Hint
Use square brackets [] to create the array and separate each title with commas.
2
Set the preview length
Create a variable called previewLength and set it to 10 to define how many characters to extract from each title.
PHP
Hint
Use the = sign to assign the number 10 to the variable previewLength.
3
Extract substrings from each title
Use a foreach loop with variable $title to go through $books. Inside the loop, create a variable $preview that uses substr($title, 0, $previewLength) to get the first 10 characters of each title.
PHP
Hint
Use foreach ($books as $title) to loop, and substr() to get the substring.
4
Print the preview snippets
Inside the foreach loop, add a print statement to display $preview followed by a newline using "\n".
PHP
Hint
Use print($preview . "\n") to show each preview on its own line.
Practice
(1/5)
1. What does the PHP function substr($text, 2, 3) do?
easy
A. Extracts the first 3 characters of $text
B. Extracts 2 characters from $text starting at index 3
C. Extracts 3 characters from $text starting at index 2
D. Extracts the last 3 characters of $text
Solution
Step 1: Understand substr parameters
The first parameter is the string, the second is the start index (0-based), and the third is the length of substring.
Step 2: Apply to given function
Starting at index 2 means the third character, and length 3 means extract 3 characters from there.
Final Answer:
Extracts 3 characters from $text starting at index 2 -> Option C
Quick Check:
substr(string, 2, 3) = 3 chars from index 2 [OK]
Hint: Remember start is zero-based, length is how many chars [OK]
Common Mistakes:
Confusing start index with length
Thinking start counts from 1 instead of 0
Assuming length is mandatory
2. Which of the following is the correct syntax to extract a substring from a string $str starting at position 4 with length 5 in PHP?
easy
A. substr($str, 5);
B. substr($str, 4, 5);
C. substr($str, 4);
D. substr($str, 5, 4);
Solution
Step 1: Recall substr syntax
substr(string, start, length) extracts substring starting at 'start' index with 'length' characters.
Step 2: Match parameters to question
Start is 4, length is 5, so substr($str, 4, 5) is correct syntax.
Final Answer:
substr($str, 4, 5); -> Option B
Quick Check:
substr($str, 4, 5) = correct syntax [OK]
Hint: Syntax is substr(string, start, length) with start zero-based [OK]
Common Mistakes:
Swapping start and length values
Omitting length when needed
Using 1-based start index
3. What will be the output of the following PHP code?
$text = "Hello World!";
echo substr($text, 6, 5);
medium
A. World
B. World!
C. Hello
D. orld!
Solution
Step 1: Identify substring start and length
Start index is 6 (0-based), length is 5 characters.
Step 2: Extract substring from "Hello World!"
Index 6 is 'W', next 5 chars are 'World'.
Final Answer:
World -> Option A
Quick Check:
substr("Hello World!", 6, 5) = "World" [OK]
Hint: Count characters from zero, then take length chars [OK]
Common Mistakes:
Including the exclamation mark mistakenly
Starting count from 1 instead of 0
Confusing length with end index
4. The following PHP code is intended to extract the substring "cat" from the string "concatenate". What is wrong?
$word = "concatenate";
echo substr($word, 3, 4);
medium
A. The length parameter is too long; it should be 3
B. The start index should be 4 instead of 3
C. The substr function cannot extract "cat" from this string
D. There is no error; the code works correctly
Solution
Step 1: Locate substring "cat" in "concatenate"
"cat" starts at index 3: c(3), a(4), t(5).
Step 2: Check length parameter
Length 4 extracts "cate" (4 chars), but "cat" is only 3 chars, so length should be 3.
Final Answer:
The length parameter is too long; it should be 3 -> Option A
Quick Check:
substr("concatenate", 3, 3) = "cat" [OK]
Hint: Count exact substring length to avoid extra chars [OK]
Common Mistakes:
Using wrong start index
Assuming substr auto-trims extra chars
Thinking substr can't extract middle parts
5. You have a string $data = "user:john;id:12345;role:admin";. How do you extract the user name "john" using substr and strpos functions in PHP?