Web Hosting Talk







View Full Version : Simple PHP Character stripping help


jon31
05-07-2005, 01:17 PM
Hey guys,

This is a really simple question, and I searched for it here, but couldn't find anything. All I want to do is strip all the characters after an @ sign, so it would change an e-mail address from user@domain.com to just user@

Think someone could help me out?

Thanks,
Jon

mouldy_punk
05-07-2005, 01:33 PM
$string = 'user@domain.com';
$new = explode('@',$string);
$domain = $new[0];
echo $domain;

Will echo everything after the @ sign.

jon31
05-07-2005, 01:38 PM
I'm embarrased by how easy that was and that I should have known. Thanks though, I appreciate it. Hopefully not too many people read this thread :)

hiryuu
05-07-2005, 05:14 PM
You can even assign directly to the domain:
$string = 'user@domain.com';
list($domain) = explode('@',$string);
echo $domain;

jon31
05-07-2005, 05:48 PM
Excellent. Thanks for the tips :)

intransit
05-07-2005, 11:08 PM
Is something wrong with my PHP install? Both of those examples print out "user" instead of "domain.com" ... using

$string = 'user@domain.com';
list($user, $domain) = explode('@',$string);
echo $domain;

prints correctly. This just me?

jon31
05-07-2005, 11:14 PM
I get user too, and that's what I wanted. Not sure if it's supposed to do the opposite.

intransit
05-08-2005, 12:12 AM
Sorry, when mouldy_punk said "Will echo everything after the @ sign." and it didn't I got confused. My mistake.

hiryuu
05-08-2005, 02:15 AM
Yup, the variable was misnamed. The OP asked for the front portion, which the code did return, and your code gives both sides.

mouldy_punk
05-08-2005, 12:56 PM
Heh, sorry, typo.

Change this line;
$domain = $new[0];
To;
$domain = $new[1];

That will echo everything after the @ sign.