Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing @email.com from string in php

Tags:

php

I want to just get the left half of an email address ( the username part of [email protected] ), so stripping the @ and any characters after it.

like image 927
daihovey Avatar asked Apr 01 '11 06:04

daihovey


3 Answers

If you have PHP5.3 you could use strstr

$email = '[email protected]';

$username = strstr($email, '@', true); //"username"

If not, just use the trusty substr

$username = substr($email, 0, strpos($email, '@'));
like image 69
chriso Avatar answered Sep 28 '22 21:09

chriso


$parts=explode('@','[email protected]');

echo $parts[0];// username
echo $parts[1];// email.com
like image 32
Shakti Singh Avatar answered Sep 28 '22 21:09

Shakti Singh


you can split the string using explode()

$email = '[email protected]';
/*split the string bases on the @ position*/
$parts = explode('@', $email);
$namePart = $parts[0];
like image 31
JohnP Avatar answered Sep 28 '22 23:09

JohnP