How to truncate the local part of email on "abc ... @ gmail.com"

I use this little function to trim lines if necessary:

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

I need help creating a new feature for trimming local parts of email, similar to what Google Groups did.

 abc...@gmail.com 

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

 apps+2189712.12457.7b00f3c9e8bfabbeea8f73@proxymail.facebook.com 

I assume that this new function will use regex to search for @ and then crop the local part by a certain number of characters to generate something like

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

Any suggestions to fix this?

Thanks!

+6
source share
1 answer

This function truncates the first part of the message (if @ is found) and the other line if @ is 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... 
+11
source

Source: https://habr.com/ru/post/900329/


All Articles