0PricingLogin
PHP Academy · Lesson

Matching with preg_match

Test strings against patterns and capture groups.

preg_match() Signature

preg_match($pattern, $subject, &$matches) returns 1 if the pattern matches, 0 if it does not, or false on error.

Simple Match Test

Use preg_match as a boolean test — return value 1 means found.

<?php
$email = "user@example.com";
if (preg_match('/\w+@\w+\.\w+/', $email)) {
    echo "Looks like an email";
}

All lessons in this course

  1. Regex Syntax Fundamentals
  2. Matching with preg_match
  3. Replacing Text with preg_replace
  4. Splitting and Practical Patterns
← Back to PHP Academy