0
0
PHPprogramming~20 mins

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

Choose your learning style9 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.