Solved

PHP strip down email address to domain name

Posted on 2011-03-02
5
520 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 109

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 109

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

815 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

8 Experts available now in Live!

Get 1:1 Help Now