-1

I have to create a nickname from the email address of the user. For example: [email protected]

in this case the nickname that I have to create should contain only alphanumeric characters: mariorossi79

How can I do that?

Istvan Szasz
  • 1,307
  • 17
  • 31
Carol Casta
  • 75
  • 1
  • 9

2 Answers2

2

Try this:

$email =  '[email protected]';
$arr = explode("@", $email);
$result = preg_replace("/[^a-zA-Z0-9]+/", "", $arr[0]);
echo $result;
Ranjan
  • 263
  • 2
  • 11
0

you can do it in 2 passes. first remove the @domain second remove the none alpha numeric.

$email = "[email protected]";
$user = preg_replace("/@.*$/", "", $email);
$nickname = preg_replace("/[^A-Za-z0-9]/", "", $user);
DevZer0
  • 13,433
  • 7
  • 27
  • 51