Saturday, March 27, 2010

Extracting username from email address

Usually we extract username using a combination of functions. Let me show you the example.

1. Using substr_replace() and strpos:
<?php
$email = 'vijithbk@gmail.com';
echo substr_replace($email, "", strpos($email, "@"));
?>
// Output
vijithbk

2. Using strstr => this is available php 5.3.1+
<?php
$email = 'vijithbk@gmail.com';
echo strstr($email,"@",true);
?>
// Output
vijithbk

Is that simple??
if you have got some other simple methods dnt forget to post it as comments

1 comment:

Anonymous said...

Jith,

the link you gave really worked. I was woking on a serious project had not enough time to think for a function. Googling revealed your page first. Keep the good work.
Thanks

Post a Comment