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

Sunday, September 16, 2007

php strtok( ) function

Hi, its been a lot of time I have been working in php. Although i had been using this function for a long time I was quite unaware about its importance. This made me put a post about this nice function.

strtok() splits a string into smaller strings usually called as tokens, with each token being delimited by any character from token. That is, if you have a string like "This is vijithbk" you could tokenize this string into its small words by using the space character as the token.


$string = "This is vijith";

$tok = strtok($string, " ");

while (
$tok !== false)
{
echo
"Word=$tok \n";
$tok = strtok(" ");
}
//Output

Word=This Word=is Word=vijith