📖 Scenario: You have a paragraph of text and want to find all the words that start with the letter 'a'.
🎯 Goal: Build a PHP script that uses preg_match_all to find all words starting with 'a' in a given text.
📋 What You'll Learn
Create a string variable called text with the exact sentence: "An apple a day keeps the doctor away."
Create a variable called pattern that holds the regex pattern to find words starting with 'a' (case-insensitive).
Use preg_match_all with pattern and text to find all matches and store them in matches.
Print the array matches[0] which contains all matched words.
💡 Why This Matters
🌍 Real World
Finding all occurrences of a pattern in text is useful in text analysis, search engines, and data extraction.
💼 Career
Many programming jobs require working with text data and using regular expressions to extract information efficiently.
Progress0 / 4 steps
1
DATA SETUP: Create the text string
Create a string variable called text and set it to the exact sentence: "An apple a day keeps the doctor away."
PHP
Hint
Use double quotes to create the string exactly as shown.
2
CONFIGURATION: Create the regex pattern
Create a variable called pattern and set it to the regex pattern "/\ba\w*/i" to find words starting with 'a' (case-insensitive).
PHP
Hint
The pattern /\ba\w*/i means words starting with 'a' ignoring case.
3
CORE LOGIC: Use preg_match_all to find matches
Use preg_match_all with pattern and text to find all matches and store them in a variable called matches.
PHP
Hint
Use preg_match_all with the pattern, text, and matches variables.
4
OUTPUT: Print the matched words
Print the array matches[0] which contains all matched words using print_r.
PHP
Hint
Use print_r($matches[0]) to display the matched words array.
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
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.
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.
Final Answer:
Finds all matches of a pattern in a string and stores them in an array. -> Option D
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
Step 1: Recall function parameter order
The correct order is pattern first, then the string to search (subject), then the array to store matches.
Step 2: Verify options
Only preg_match_all($pattern, $subject, $matches); follows this order: preg_match_all($pattern, $subject, $matches);
Final Answer:
preg_match_all($pattern, $subject, $matches); -> Option B
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?
The pattern /\b\w{6}\b/ matches words with exactly 6 letters.
Step 2: Check the words in the string
"apple" has 5 letters, "banana" has 6, "cherry" has 6. So only "banana" and "cherry" match.
Final Answer:
["banana", "cherry"] -> Option C
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
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.
Step 2: Identify the impact
Passing $matches as the subject and $text as matches causes wrong behavior and no matches found.
Final Answer:
The order of parameters is incorrect. -> Option A
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
Step 1: Analyze the email pattern requirements
Emails have letters, digits, dots, underscores before @, then domain with dot and extension.
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.
Final Answer:
'/[\w\.]+@[\w\.]+\.[a-z]{2,}/i' -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.