String Search Functions with strpos and strstr in PHP
📖 Scenario: You are building a simple text analyzer tool that helps users find specific words or phrases inside a paragraph. This tool will help users quickly locate where a word appears and extract parts of the text starting from that word.
🎯 Goal: Build a PHP script that uses strpos to find the position of a word in a sentence and strstr to extract the part of the sentence starting from that word.
📋 What You'll Learn
Create a string variable with a fixed sentence.
Create a variable holding the word to search for.
Use strpos to find the position of the word in the sentence.
Use strstr to get the part of the sentence starting from the word.
Print the position and the extracted substring.
💡 Why This Matters
🌍 Real World
Searching text is common in many applications like search engines, text editors, and data processing tools.
💼 Career
Understanding string search functions helps in web development, data parsing, and building user-friendly search features.
Progress0 / 4 steps
1
Create the sentence string
Create a string variable called sentence and set it to the exact value: "Learning PHP string functions is fun and useful."
PHP
Hint
Use double quotes to create the string exactly as shown.
2
Set the word to search for
Create a string variable called searchWord and set it to the exact value: "string"
PHP
Hint
Remember to use double quotes for the string.
3
Find the position and extract substring
Use strpos with variables sentence and searchWord to find the position of searchWord in sentence and store it in a variable called position. Then use strstr with sentence and searchWord to get the substring starting from searchWord and store it in a variable called substring.
PHP
Hint
Use the exact variable names and function calls as shown.
4
Print the results
Print the position stored in position using echo with the exact text: "Position: " followed by the position. Then print the substring stored in substring with the exact text: "Substring: " followed by the substring. Each print should be on its own line.
PHP
Hint
Use echo and concatenate strings with .. Use \n for new lines.
Practice
(1/5)
1. What does the PHP function strpos do?
easy
A. Converts a string to uppercase
B. Extracts a substring starting from a specific character
C. Finds the position of a substring inside a string
D. Replaces a substring with another string
Solution
Step 1: Understand the purpose of strpos
The strpos function searches for the first occurrence of a substring inside another string and returns its position.
Step 2: Compare with other options
Options A, B, and C describe different string functions like strtoupper, strstr, and str_replace, which are not what strpos does.
Final Answer:
Finds the position of a substring inside a string -> Option C
Quick Check:
strpos = position finder [OK]
Hint: strpos returns position index of substring [OK]
Common Mistakes:
Confusing strpos with strstr
Thinking strpos extracts substring
Assuming strpos replaces text
2. Which of the following is the correct syntax to find the position of 'cat' in the string $text = 'concatenate' using strpos?
easy
A. strpos('concatenate', 'cat')
B. strpos($text)
C. strpos('cat', $text)
D. strpos($text, 'cat')
Solution
Step 1: Identify correct parameter order for strpos
The first parameter is the full string, the second is the substring to find. So strpos($text, 'cat') is correct.
Step 2: Check other options for errors
strpos('concatenate', 'cat') uses a string literal instead of the variable. strpos('cat', $text) reverses parameters. strpos($text) misses the second parameter.
Final Answer:
strpos($text, 'cat') -> Option D
Quick Check:
strpos(string, substring) [OK]
Hint: First argument is full string, second is substring [OK]
A. strpos returns false if found, so condition is wrong
B. strpos returns 0 for 'apple' position, which is treated as false
C. Missing third parameter in strpos
D. Syntax error in if statement
Solution
Step 1: Understand strpos return values
strpos returns 0 if the substring is found at the start (index 0). In PHP, 0 is treated as false in conditions.
Step 2: Analyze the if condition behavior
The condition if (strpos(...)) fails when strpos returns 0, so it wrongly goes to else.
Final Answer:
strpos returns 0 for 'apple' position, which is treated as false -> Option B
Quick Check:
Check strpos !== false to avoid 0 as false [OK]
Hint: Check strpos !== false, not just if() [OK]
Common Mistakes:
Using if(strpos()) without strict false check
Assuming strpos returns boolean true/false only
Ignoring 0 as valid position
5. Given the string $sentence = 'The quick brown fox jumps', which code snippet correctly extracts the substring starting from the word 'brown' using strstr?
hard
A. echo strstr($sentence, 'brown');
B. echo strstr($sentence, 'brown', true);
C. echo strpos($sentence, 'brown');
D. echo substr($sentence, strpos($sentence, 'brown'));
Solution
Step 1: Understand strstr behavior
strstr returns the part of the string starting from the first occurrence of the substring, including it.
Step 2: Analyze options
echo strstr($sentence, 'brown'); correctly uses strstr to get substring from 'brown'. echo strstr($sentence, 'brown', true); returns part before 'brown'. echo strpos($sentence, 'brown'); returns position, not substring. echo substr($sentence, strpos($sentence, 'brown')); uses substr with strpos, which also works but is not strstr.
Final Answer:
echo strstr($sentence, 'brown'); -> Option A
Quick Check:
strstr returns substring from needle [OK]
Hint: strstr returns substring from needle, true returns before [OK]
Common Mistakes:
Using third parameter true to get substring after needle
Confusing strpos with strstr
Expecting strpos to return substring
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.