如何提取“@”前面的所有文本字符串中的字符

发布于 2024-11-14 02:30:18 字数 472 浏览 3 评论 0原文

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

例如 [email protected],我想删除以 @ 开头的字符串所以它只变成“管理员”。 就像在 Twitter 中一样……我读到了有关 str 替换和修剪的内容,但我认为还有其他方法可以做到吗?

$email = '[email protected]';

echo substr_replace($email, ?, ?) ; this i cant do

how to remove a string starting from '@'?

for example [email protected], 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 = '[email protected]';

echo substr_replace($email, ?, ?) ; this i cant do

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

尛丟丟 2024-11-21 02:30:19
$email = '[email protected]';
list($Lastpart,$Firstpart) = explode("@",$email); 

echo $Firstpart; //before @ sign
echo $Lastpart;  //after @ sign
$email = '[email protected]';
list($Lastpart,$Firstpart) = explode("@",$email); 

echo $Firstpart; //before @ sign
echo $Lastpart;  //after @ sign
锦爱 2024-11-21 02:30:19
$email = explode("@", $email);

$name = $email[0];
$email = explode("@", $email);

$name = $email[0];
听风吹 2024-11-21 02:30:19
$email = '[email protected]';
$aEmail= explode('@',$email);
echo $aEmail[0];
$email = '[email protected]';
$aEmail= explode('@',$email);
echo $aEmail[0];
恰似旧人归 2024-11-21 02:30:18

您不需要替换其余部分,您只需剪切直到搜索到的字符即可。在这种情况下,使用 strtok 非常简单:

 $name = strtok($email, "@");

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, "@");
似狗非友 2024-11-21 02:30:18

怎么样:

substr($email, 0, strpos($email, '@'));

What about:

substr($email, 0, strpos($email, '@'));
じ违心 2024-11-21 02:30:18

strtok() 是最好的,但作为替代方案......

$name = strstr($email, '@', TRUE);

strtok() is the best, but as an alternative...

$name = strstr($email, '@', TRUE);
未蓝澄海的烟 2024-11-21 02:30:18

试试这个函数:strstr

$start = strstr($email, '@', true);

Try this function: strstr:

$start = strstr($email, '@', true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文