Solved

PHP strip down email address to domain name

Posted on 2011-03-02
5
518 Views
Last Modified: 2012-05-11
Hi guys,

Is it possible for PHP to strip down an email address to the name of a domain name.

E.g.

info@domainname.com -> domainname

So it strips off everything infront of the @ symbol (including the @ symbol)
And also strips off everything after the name in the domain name, so no .com.etc (no extensions)

Need one simple line of code in PHP to do this if possible.

Thanks
0
Comment
Question by:dev09
5 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 35016335
<?php

$email = "info@domainname.com";

$domain = preg_replace( '!^.+?@(.+?)$!', '$1', $email );

echo "Domain is $domain";
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 35017083
another way is , use explode()

$pizza  = "info@domainname.com";
$pieces = explode("@", $pizza);

echo $pieces[1]; // domainname.com



0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 35019022
You might be interested in this excellent book.  Very readable with great code examples.
http://www.sitepoint.com/books/phpmysql4/

My version of the solution would be this:

$domain = end(explode('@', $email_address));
0
 
LVL 9

Expert Comment

by:crazedsanity
ID: 35027284
The original request was to not have any extensions.  Expanding the code from bportlock, here's what you want, all be it in multiple lines::::
<?php

$email = "info@domainname.com";

$domain = preg_replace( '!^.+?@(.+?)$!', '$1', $email );
$bits = explode('.', $domain);
$domain = $bits[(count($bits) -2)];

echo "Domain is $domain";

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 35032627
Ahh, good point about the "no extensions" part.  In that case my one-liner would be this:

$domain = current(explode('.', end(explode('@', $email_address))));

But that said, I am not sure of the value of discarding the TLD.  foo.com and foo.org are different URLs, as different as google.com and yahoo.com.  Email will be routed by different MX records.  Throwing away the TLD is akin to a mung operation -- we try to avoid those in professional programming.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now