?
Solved

PHP strip down email address to domain name

Posted on 2011-03-02
5
Medium Priority
?
534 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 2000 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 111

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 111

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

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

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

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 …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses

777 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