问题
how to remove a string starting from '@'?
for example admin@admin.com, i want to remove the string starting from @ so it becomes 'admin' only. just like in twitter..i read about str replace and trim but i think theres other way to do it?
$email = 'admin@admin.com';
echo substr_replace($email, ?, ?) ; this i cant do
回答1:
You don't need to replace the remainder, you can just cut out until the searched character. In this case it's very easy with strtok:
$name = strtok($email, "@");
回答2:
What about:
substr($email, 0, strpos($email, '@'));
回答3:
strtok()
is the best, but as an alternative...
$name = strstr($email, '@', TRUE);
回答4:
Try this function: strstr:
$start = strstr($email, '@', true);
回答5:
$email = 'admin@admin.com';
list($Lastpart,$Firstpart) = explode("@",$email);
echo $Firstpart; //before @ sign
echo $Lastpart; //after @ sign
回答6:
$email = explode("@", $email);
$name = $email[0];
回答7:
$email = 'admin@admin.com';
$aEmail= explode('@',$email);
echo $aEmail[0];
来源:https://stackoverflow.com/questions/6273679/how-to-extract-all-text-in-front-of-the-character-in-a-string