Solved

PHP strip down email address to domain name

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…

758 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

22 Experts available now in Live!

Get 1:1 Help Now