Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find all matches of 'cat' in the string.
PHP
<?php $subject = "The cat sat on the cat mat."; preg_match_all([1], $subject, $matches); print_r($matches[0]); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the pattern without slashes causes an error.
Adding 'g' modifier is invalid in PHP regex.
✗ Incorrect
The pattern must be enclosed in slashes to be a valid regex in PHP. So "/cat/" is correct.
2fill in blank
mediumComplete the code to perform a case-insensitive global match for 'dog'.
PHP
<?php $subject = "Dog dog DOG doG"; preg_match_all([1], $subject, $matches); print_r($matches[0]); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'g' modifier which is invalid in PHP.
Not using slashes around the pattern.
✗ Incorrect
The 'i' modifier makes the match case-insensitive. PHP does global matching by default with preg_match_all.
3fill in blank
hardFix the error in the code to correctly find all numbers in the string.
PHP
<?php $subject = "123 abc 456 def 789"; preg_match_all([1], $subject, $matches); print_r($matches[0]); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not escaping the backslash causes an error.
Using '\D' matches non-digits, which is wrong.
✗ Incorrect
The pattern "/\d+/" matches one or more digits. The backslash must be escaped in PHP strings.
4fill in blank
hardFill both blanks to find all words starting with 'a' or 'A' in the string.
PHP
<?php $subject = "Apple and apricot are awesome."; preg_match_all([1], $subject, $matches, [2]); print_r($matches[0]); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using PREG_SET_ORDER changes the structure of matches.
Not escaping backslashes in the pattern.
✗ Incorrect
The pattern matches words starting with 'a' or 'A'. PREG_PATTERN_ORDER organizes matches by pattern order.
5fill in blank
hardFill all three blanks to extract all email addresses from the text.
PHP
<?php $text = "Contact us at support@example.com or sales@example.org."; preg_match_all([1], $text, $matches, [2], [3]); print_r($matches[0]); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong offset or flags changes output structure.
Incorrect regex pattern misses valid emails.
✗ Incorrect
The regex matches emails. Offset 0 means start at beginning. PREG_PATTERN_ORDER organizes matches by pattern.