Using preg_match_all for Global Matching in PHP
📖 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