Character classes and quantifiers
📖 Scenario: You are working on a simple PHP script to check if certain words match a pattern. This is useful when you want to find words that follow specific rules, like starting with a vowel or having a certain number of letters.
🎯 Goal: Build a PHP script that uses regular expressions with character classes and quantifiers to find words that start with a vowel and have exactly 5 letters.
📋 What You'll Learn
Create a list of words in a PHP array called
$words with the exact values: 'apple', 'banana', 'grape', 'orange', 'pear', 'peach'Create a variable called
$pattern that holds a regular expression to match words starting with a vowel (a, e, i, o, u) and having exactly 5 lettersUse a
foreach loop with the variable $word to go through each word in $wordsInside the loop, use
preg_match with $pattern and $word to check if the word matches the patternPrint each matching word on its own line using
echo💡 Why This Matters
🌍 Real World
Regular expressions help filter and find text patterns in data, like validating user input or searching documents.
💼 Career
Knowing how to use character classes and quantifiers in regex is useful for web developers, data analysts, and anyone working with text processing.
Progress0 / 4 steps