Check if String is an Email Address in PHP

This tutorial will discuss about unique ways to check if string is an email address in php.

Table Of Contents

To check if string is a valid email address or not we are going to use two methods in PHP.

Method 1: Using filter_var() with FILTER_VALIDATE_EMAIL

The filter_var() function accepts a string and a filter variable as an argument. It returns the filtered data or false if the given filter does not apply on the string.

So. to check if a string contains a valid email address or not we can pass string as the first argument in the filter_var() function and as the second argument we can pass a filter field FILTER_VALIDATE_EMAIL. If it returns false value, then it means that the given string does not contain valid email address. Ootherwise if it returns true not known false value then it means that the given string contains a valid email address.

Let’s see the complete example,

<?php
/**
 * Checks if a string represents a valid email address
 * using filter_var() with FILTER_VALIDATE_EMAIL.
 *
 * @param string $str The string to check.
 * @return bool True if the string represents a valid email address, false otherwise.
 */
function isEmail($str)
{
    return filter_var($str, FILTER_VALIDATE_EMAIL) !== false;
}

$emailAddress = "[email protected]";

// Check if string contains a valid email address
if (isEmail($emailAddress)) {
    echo "The string represents a valid email address.";
} else {
    echo "The string does not represent a valid email address.";
}
?>

Output

The string represents a valid email address.

Method 2: Using Regular Expression

We can also use the regular expression to check if a given string contains a valid email address or not. For this we are going to use this below mentioned regex pattern,

'/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/i'

We can pass this regex pattern and the string as the arguments into the preg_match() function of PHP. If it returns 1, then it means that the given string contains a valid email address in PHP.

We have created a separate function for this,

function isValidEmail($str)
{
    // regex pattern to check if string contains valid email address
    $pattern = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/i';

    // check if string matches the regex pattern
    return preg_match($pattern, $str) === 1;
}

It accepts string is an argument and returns true if the given string contains a valid email address in PHP.

Let’s see the complete example,

<?php
/**
 * Checks if a string represents a valid email address using a
 * regular expression (preg_match()).
 *
 * @param string $str The string to check.
 * @return bool True if the string represents a valid email address, false otherwise.
 */
function isValidEmail($str)
{
    // regex pattern to check if string contains valid email address
    $pattern = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/i';

    // check if string matches the regex pattern
    return preg_match($pattern, $str) === 1;
}


$emailAddress = "[email protected]";

// Check if string contains a valid email address
if (isValidEmail($emailAddress)) {
    echo "The string represents a valid email address.";
} else {
    echo "The string does not represent a valid email address.";
}
?>

Output

The string represents a valid email address.

Summary

We learn about two different ways to check if string contains a valid email address in PHP.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top