Bird
Raised Fist0
PHPprogramming~20 mins

Preg_match_all for global matching in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Preg_match_all Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of preg_match_all with a simple pattern?
Consider the following PHP code snippet using preg_match_all. What will be the output?
PHP
<?php
$subject = 'apple banana apple cherry';
$pattern = '/apple/';
preg_match_all($pattern, $subject, $matches);
print_r($matches[0]);
?>
AArray ( [0] => apple )
BArray ( [0] => apple banana apple cherry )
CArray ( [0] => apple [1] => apple )
DArray ( )
Attempts:
2 left
💡 Hint
preg_match_all finds all matches, not just the first one.
❓ Predict Output
intermediate
2:00remaining
How many matches does preg_match_all find?
Given this PHP code, how many matches will preg_match_all find?
PHP
<?php
$subject = 'cat bat rat mat';
$pattern = '/.at/';
$count = preg_match_all($pattern, $subject, $matches);
echo $count;
?>
A4
B3
C1
D0
Attempts:
2 left
💡 Hint
The pattern matches any character followed by 'at'.
🔧 Debug
advanced
2:00remaining
Why does this preg_match_all code produce an empty array?
Look at this PHP code. Why does preg_match_all return no matches?
PHP
<?php
$subject = '123-456-7890';
$pattern = '/\d{3}-\d{3}-\d{4}/';
preg_match_all($pattern, $subject, $matches);
print_r($matches[0]);
?>
AIt returns an empty array because the pattern is correct but preg_match_all expects global flag 'g' which PHP does not use.
BIt returns an empty array because the pattern is missing delimiters.
CIt returns an empty array because the subject string does not contain the pattern.
DIt returns an empty array because the backslashes are not escaped properly in the pattern string.
Attempts:
2 left
💡 Hint
Check how backslashes are used inside PHP strings for regex.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in preg_match_all usage?
Identify which PHP code snippet will cause a syntax error when using preg_match_all.
Apreg_match_all('/foo/', $text, $matches, 'PREG_OFFSET_CAPTURE');
Bpreg_match_all('/foo/', $text, $matches, PREG_OFFSET_CAPTURE);
Cpreg_match_all('/foo/', $text, $matches);
Dpreg_match_all('/foo/', $text, $matches, 0);
Attempts:
2 left
💡 Hint
Check the type of the fourth argument in preg_match_all.
🚀 Application
expert
3:00remaining
Extract all email addresses from a text using preg_match_all
You want to extract all email addresses from a string using preg_match_all. Which code snippet correctly captures all emails?
A
$pattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g';
preg_match_all($pattern, $text, $matches);
B
$pattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/';
preg_match_all($pattern, $text, $matches);
C
$pattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/i';
preg_match_all($pattern, $text, $matches);
D
$pattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/';
preg_match($pattern, $text, $matches);
Attempts:
2 left
💡 Hint
Remember PHP regex does not use 'g' flag and preg_match_all finds all matches.

Practice

(1/5)
1. What does the PHP function preg_match_all do?
easy
A. Splits a string into an array using a pattern.
B. Finds only the first match of a pattern in a string.
C. Replaces all matches of a pattern in a string with another string.
D. Finds all matches of a pattern in a string and stores them in an array.

Solution

  1. Step 1: Understand the function purpose

    preg_match_all is designed to find every occurrence of a pattern in a string, not just the first one.
  2. Step 2: Compare with other functions

    preg_match finds only the first match, preg_replace replaces matches, and preg_split splits strings. So only preg_match_all finds all matches and stores them.
  3. Final Answer:

    Finds all matches of a pattern in a string and stores them in an array. -> Option D
  4. Quick Check:

    preg_match_all = all matches found [OK]
Hint: preg_match_all finds every match, not just one [OK]
Common Mistakes:
  • Confusing preg_match_all with preg_match
  • Thinking it replaces matches
  • Assuming it splits strings
2. Which of the following is the correct syntax to use preg_match_all in PHP?
easy
A. preg_match_all($subject, $pattern, $matches);
B. preg_match_all($pattern, $subject, $matches);
C. preg_match_all($pattern, $matches, $subject);
D. preg_match_all($matches, $pattern, $subject);

Solution

  1. Step 1: Recall function parameter order

    The correct order is pattern first, then the string to search (subject), then the array to store matches.
  2. Step 2: Verify options

    Only preg_match_all($pattern, $subject, $matches); follows this order: preg_match_all($pattern, $subject, $matches);
  3. Final Answer:

    preg_match_all($pattern, $subject, $matches); -> Option B
  4. Quick Check:

    Pattern, subject, matches array order [OK]
Hint: Pattern comes first, then string, then matches array [OK]
Common Mistakes:
  • Swapping pattern and subject parameters
  • Placing matches array before subject
  • Using wrong parameter order
3. What will be the output of the following PHP code?
$text = "apple, banana, cherry";
preg_match_all('/\b\w{6}\b/', $text, $matches);
print_r($matches[0]);
medium
A. ["apple"]
B. ["apple", "banana", "cherry"]
C. ["banana", "cherry"]
D. []

Solution

  1. Step 1: Understand the regex pattern

    The pattern /\b\w{6}\b/ matches words with exactly 6 letters.
  2. Step 2: Check the words in the string

    "apple" has 5 letters, "banana" has 6, "cherry" has 6. So only "banana" and "cherry" match.
  3. Final Answer:

    ["banana", "cherry"] -> Option C
  4. Quick Check:

    Words with 6 letters = banana, cherry [OK]
Hint: Count letters to match \w{6} exactly [OK]
Common Mistakes:
  • Including words with length not equal to 6
  • Misunderstanding \b word boundaries
  • Confusing \w with any character
4. Identify the error in this PHP code snippet using preg_match_all:
$text = "one two three";
preg_match_all('/\w+/', $matches, $text);
print_r($matches);
medium
A. The order of parameters is incorrect.
B. The regex pattern is invalid.
C. The variable $matches is not initialized.
D. The print_r function cannot print arrays.

Solution

  1. Step 1: Check parameter order in preg_match_all

    The correct order is pattern, subject string, then matches array. Here, $matches and $text are swapped.
  2. Step 2: Identify the impact

    Passing $matches as the subject and $text as matches causes wrong behavior and no matches found.
  3. Final Answer:

    The order of parameters is incorrect. -> Option A
  4. Quick Check:

    Parameter order matters in preg_match_all [OK]
Hint: Remember: pattern, subject, matches array order [OK]
Common Mistakes:
  • Swapping subject and matches parameters
  • Assuming regex pattern is wrong
  • Thinking print_r can't print arrays
5. You want to extract all email addresses from a text using preg_match_all. Which pattern correctly matches a simple email format (letters, digits, dots, underscores before @, then domain)?
hard
A. '/[\w\.]+@[\w\.]+\.[a-z]{2,}/i'
B. '/\d+@[a-z]+\.[a-z]{2,}/'
C. '/[a-z]+@[a-z]+\.[a-z]{3}/'
D. '/\w+\s+@\s+\w+\.com/'

Solution

  1. Step 1: Analyze the email pattern requirements

    Emails have letters, digits, dots, underscores before @, then domain with dot and extension.
  2. Step 2: Check each pattern

    '/[\w\.]+@[\w\.]+\.[a-z]{2,}/i' matches word characters and dots before @, then domain and extension with at least 2 letters, case insensitive. Others are too restrictive or incorrect.
  3. Final Answer:

    '/[\w\.]+@[\w\.]+\.[a-z]{2,}/i' -> Option A
  4. Quick Check:

    Pattern includes letters, digits, dots, underscores [OK]
Hint: Use \w and dot before @, domain with dot and 2+ letters [OK]
Common Mistakes:
  • Using \d+ only before @ (digits only)
  • Forcing exact 3-letter domain extensions
  • Adding spaces inside email pattern
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard