如何提取字符串中“@"字符前面的所有文本 [英] How to extract all text in front of the '@' character in a string

查看:64
本文介绍了如何提取字符串中“@"字符前面的所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除以@"开头的字符串?

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, "@");

这篇关于如何提取字符串中“@"字符前面的所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆