How to extract domain name from email address

Retrieve the domain name from an email address

This tutorial shows you on how to extract the domain name from an email address in different programming languages, such as PHP, Java, VB .NET, C#, Python, Javascript and Ruby programming language. The below sample code is useful when you need to extract the domain name to be supplied into FraudLabs Pro REST API (for email_domain field) or any other project requirements.

Extract Domain in PHP

<?php
$email = 'user@example.com';
$domain = substr($email, strpos($email, '@') + 1);
?>

Extract Domain in Java

String email = "user@example.com";
String domain = email .substring(email .indexOf("@") + 1);

Extract Domain in VB.NET

Dim email As String = "user@example.com"
Dim startIndex As Integer = email.IndexOf("@")
Dim endIndex As Integer = email.IndexOf(".", startIndex)

Dim domain As String = email.SubString(startIndex + 1, endIndex)

Extract Domain in C#

string email = "user@example.com";
int indexOfAt = email.IndexOf('@');
string domain = email.Substring(indexOfAt + 1);

Extract Domain in Python

email = 'user@example.com'
domain = email.split('@')[1]

Extract Domain in Javascript

const emailAddress = "example@example.com";

// Extract the domain
const domain = emailAddress.split('@')[1];

console.log(domain);

Extract Domain in Ruby

email_address = "example@example.com"

# Extract the domain using a regular expression
domain = email_address.match(/@(.+)$/)
puts domain[1]

Ready to start with FraudLabs Pro?

Get Micro plan for free, you can quickly explore and integrate with our fraud prevention solution in minutes.

Was this article helpful?

Related Articles