简体   繁体   中英

How to truncate an email local-part to 'abc…@gmail.com'

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.

abc...@gmail.com

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

apps+2189712.12457.7b00f3c9e8bfabbeea8f73@proxymail.facebook.com

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

apps+21...@proxymail.facebook.com

Any suggestions how to tackle this?

Thanks!

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('apps+2189712.12457.7b00f3c9e8bfabbeea8f73@proxymail.facebook.com', 10);
// will output : apps+21897...@proxymail.facebook.com

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM