Pattern Matching in PHP-(preg_match)

Pattern Matching in PHP helps you use regular expressions with preg_match. Learn regex syntax, delimiters, and practical examples for PHP developers.
Pattern Matching in PHP | Regular Expressions with preg_match
Syntax of preg_match() in PHP
preg_match(pattern, subject, matches, flags, offset);
Parameter Explanation:
pattern → The regular expression pattern (enclosed in delimiters).
subject → The string where the pattern is searched.
matches (optional) → An array that stores matched results.
flags (optional) → Modifiers that change search behavior (e.g., PREG_OFFSET_CAPTURE).
offset (optional) → The position in the subject where the search starts.
Common Delimiters and Their Meaning:
😊 \. (Escaped Dot)
The escaped dot (\.) is used to ensure that there is a literal dot (.) in the pattern.
In regex, the dot (.) usually means “any character,” so we use the backslash (\) to escape it and make it a literal dot.
📌 Example Matches:
✅ gmail.
✅ yahoo.
❌ Invalid Example:
user@gmailcom → (Missing . before com)
[syntaxhighlighter lang=”php”]%3C?php if (preg_match(“/gmail\./”, “user@gmail.com”)) { echo “Match found!”; } else { echo “No match.”; } ?%3E[/syntaxhighlighter]