Bird
0
0

Given the string $data = 'user@example.com', how can you extract the domain part (after '@') using strstr?

hard📝 Application Q9 of 15
PHP - String Functions
Given the string $data = 'user@example.com', how can you extract the domain part (after '@') using strstr?
A$domain = strstr($data, '@', true);
B$domain = strpos($data, '@');
C$domain = strstr($data, '@') + 1;
D$domain = strstr($data, '@');
Step-by-Step Solution
Solution:
  1. Step 1: Understand strstr with third parameter

    By default, strstr returns substring from first occurrence to end, including the searched character.
  2. Step 2: Apply to extract domain

    $domain = strstr($data, '@'); returns '@example.com'. $domain = strstr($data, '@', true); returns part before '@'. $domain = strstr($data, '@') + 1; is invalid arithmetic. $domain = strpos($data, '@'); returns position, not substring.
  3. Final Answer:

    $domain = strstr($data, '@'); -> Option D
  4. Quick Check:

    strstr returns substring including '@' by default [OK]
Quick Trick: strstr returns substring including searched char by default [OK]
Common Mistakes:
  • Using third parameter true to get after '@'
  • Adding 1 to strstr result (string + int error)
  • Using strpos instead of strstr for substring

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes