Current Location: Home> Latest Articles> stripos for domain name judgment of email address

stripos for domain name judgment of email address

M66 2025-05-31

In PHP development, we often need to verify or analyze email addresses. For example, you may need to check whether an email address belongs to a specific domain name (such as gmail.com , m66.net , etc.). At this time, the stripos() function can come in handy.

What is stripos() ?

stripos() is a string function in PHP that finds where a substring first appears in the target string. Unlike strpos() , strpos() is case-insensitive, which is very useful when handling email addresses, because the domain part of an email address is usually case-insensitive.

The function definition is as follows:

 int|false stripos ( string $haystack , mixed $needle [, int $offset = 0 ] )
  • $haystack is the string to search for.

  • $needle is what you want to look for.

  • $offset is an optional parameter that indicates where to start the search.

If found, return the location index (starting from 0); if not found, return false .

Example: Determine whether the email address belongs to m66.net

Let's look at a practical example. Suppose you want to determine whether the email address submitted by a user belongs to the m66.net domain:

 <?php
function isM66Email($email) {
    // Extract the domain name part of the email address
    $domain = substr(strrchr($email, "@"), 1);
    
    // use stripos Determine whether it is m66.net
    if (stripos($domain, 'm66.net') !== false) {
        return true;
    }
    return false;
}

// Sample Email
$email1 = 'user123@m66.net';
$email2 = 'someone@gmail.com';

var_dump(isM66Email($email1)); // Output: bool(true)
var_dump(isM66Email($email2)); // Output: bool(false)
?>

illustrate:

  1. Use strrchr($email, "@") to get the string after the last @ in the mailbox, that is, the domain name.

  2. Use substr() to remove the beginning @ symbol.

  3. Use stripos() to find if the domain name contains m66.net .

  4. Note that stripos() returns a position index (maybe 0 ), so convergence !== false must be used to determine whether it is found.

Applicable scenarios

  • Determine whether the email address comes from a specific company or organization.

  • Batch filters registered users of a domain name.

  • Personalize the email address of different domain names.

Things to note

Although stripos() is convenient, if you only want to match the domain name exactly and do not want subdomains like abc.m66.net to be matched, it is recommended that you use a stricter string comparison method, such as: