How to extract all text in front of the '@' character in a string

牧云@^-^@ 提交于 2019-12-24 08:39:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!