📖 Scenario: Imagine you are building a simple web form in PHP where users enter their email addresses. You want to check if the emails are typed correctly before saving them. This is where regular expressions (regex) help.
🎯 Goal: You will create a PHP script that uses regex to check if an email address is valid. This will help you understand why regex is useful in PHP for pattern matching and validation.
📋 What You'll Learn
Create a variable with a sample email address
Create a regex pattern to match a simple email format
Use PHP's preg_match function to check if the email matches the pattern
Print a message showing if the email is valid or not
💡 Why This Matters
🌍 Real World
Web forms often need to check if user inputs like emails or phone numbers are correct before saving or sending data.
💼 Career
Many PHP developers use regex to validate user input, clean data, and search text efficiently in web applications.
Progress0 / 4 steps
1
DATA SETUP: Create a variable with a sample email address
Create a variable called $email and set it to the string "user@example.com".
PHP
Hint
Use the = sign to assign the email string to the variable $email.
2
CONFIGURATION: Create a regex pattern for a simple email format
Create a variable called $pattern and set it to the regex string "/^[\w.-]+@[\w.-]+\.\w+$/" which matches a basic email format.
PHP
Hint
The regex pattern checks for letters, numbers, dots, or dashes before and after the '@' symbol, followed by a dot and some letters.
3
CORE LOGIC: Use preg_match to check if the email matches the pattern
Use preg_match($pattern, $email) inside an if statement to check if $email matches $pattern. Inside the if, set a variable $valid to true, else set it to false.
PHP
Hint
Use preg_match to test the pattern and assign true or false to $valid.
4
OUTPUT: Print if the email is valid or not
Use if ($valid) to print "Email is valid." else print "Email is invalid.".
PHP
Hint
Use echo to show the message based on $valid.
Practice
(1/5)
1. Why do we need regex (regular expressions) in PHP?
easy
A. To handle file uploads
B. To create graphical user interfaces
C. To find and work with specific text patterns easily
D. To connect to databases
Solution
Step 1: Understand regex purpose
Regex is designed to find patterns in text, like searching or validating strings.
Step 2: Match purpose with PHP tasks
PHP uses regex to quickly check or change text based on patterns, not for GUIs or databases.
Final Answer:
To find and work with specific text patterns easily -> Option C
Quick Check:
Regex = text pattern matching [OK]
Hint: Regex is for text pattern tasks, not UI or databases [OK]
Common Mistakes:
Confusing regex with database functions
Thinking regex creates user interfaces
Assuming regex handles file uploads
2. Which PHP function uses regex to check if a pattern exists in a string?
easy
A. preg_match()
B. str_replace()
C. array_search()
D. file_get_contents()
Solution
Step 1: Identify regex functions in PHP
preg_match() is the PHP function that checks if a regex pattern matches a string.
Step 2: Compare with other functions
str_replace() replaces text, array_search() searches arrays, file_get_contents() reads files, none use regex for matching.
Final Answer:
preg_match() -> Option A
Quick Check:
Regex match function = preg_match() [OK]
Hint: preg_ prefix means regex functions in PHP [OK]