如何将电子邮件本地部分截断为“abc...@gmail.com”

发布于 2024-12-12 13:04:29 字数 1066 浏览 5 评论 0原文

我使用这个小函数在需要时截断字符串:

function truncate_text($text, $nbrChar = 55, $append='...') {
    if (strlen($text) > $nbrChar) {
        $text = substr($text, 0, $nbrChar);
        $text .= $append;
    } 
    return $text;
}

我需要一些帮助来创建一个新函数来截断电子邮件本地部分,类似于 Google 网上论坛中所做的操作。

[email protected]

这对于使用 Facebook 代理电子邮件的用户尤其有用。

[email protected]

我想这个新函数将使用正则表达式来查找 @ ,然后将本地部分截断为一定数量的字符以生成类似的内容有

[email protected]

什么建议如何解决这个问题吗?

谢谢!

I use this little function to truncate strings when needed:

function truncate_text($text, $nbrChar = 55, $append='...') {
    if (strlen($text) > $nbrChar) {
        $text = substr($text, 0, $nbrChar);
        $text .= $append;
    } 
    return $text;
}

I would like some help to create a new function to truncate email local-parts similar to what is done in Google Groups.

[email protected]

This would be especially useful for users using Facebook's proxy email.

[email protected]

I guess this new function would use regex to find the @ and then truncate the local-part to a certain number of characters to generate something like

[email protected]

Any suggestions how to tackle this?

Thanks!

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

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

发布评论

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

评论(1

江南月 2024-12-19 13:04:30

此函数将截断电子邮件的第一部分(如果找到@)和其他字符串(如果找不到@)。

function truncate_text($text, $nbrChar = 55, $append='...') {
  if(strpos($text, '@') !== FALSE) {
    $elem = explode('@', $text);
    $elem[0] = substr($elem[0], 0, $nbrChar) . $append;
    return $elem[0] . '@' . $elem[1];
  }
  if (strlen($text) > $nbrChar) {
    $text = substr($text, 0, $nbrChar);
    $text .= $append;
  } 
  return $text;
}

echo truncate_text('[email protected]', 10);
// will output : [email protected]

echo truncate_text('apps+2189712.12457.7b00f3c9e8bfabbeea8f73proxymail.facebook.com', 10);
// will output : apps+21897...

This function will truncate the first part of the email ( if the @ is found ) and other string if @ not found.

function truncate_text($text, $nbrChar = 55, $append='...') {
  if(strpos($text, '@') !== FALSE) {
    $elem = explode('@', $text);
    $elem[0] = substr($elem[0], 0, $nbrChar) . $append;
    return $elem[0] . '@' . $elem[1];
  }
  if (strlen($text) > $nbrChar) {
    $text = substr($text, 0, $nbrChar);
    $text .= $append;
  } 
  return $text;
}

echo truncate_text('[email protected]', 10);
// will output : [email protected]

echo truncate_text('apps+2189712.12457.7b00f3c9e8bfabbeea8f73proxymail.facebook.com', 10);
// will output : apps+21897...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文