如何提取字符串中“@"字符前面的所有文本
问题描述:
如何删除以@"开头的字符串?
how to remove a string starting from '@'?
例如admin@admin.com,我想删除以@ 开头的字符串,使其仅成为admin".就像在 twitter..我读过 str 替换和修剪,但我认为还有其他方法可以做到吗?
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
答
余数不需要替换,只要剪掉搜索到的字符即可.在这种情况下,使用 strtok 非常容易:
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, "@");