Solved

PHP strip down email address to domain name

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

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 110

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to implement server side field validation and display customized error messages to the client.
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 count occurrences of each item in an array.

724 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