[英]How to truncate an email local-part to 'abc…@gmail.com'
我使用這個小函數在需要時截斷字符串:
function truncate_text($text, $nbrChar = 55, $append='...') {
if (strlen($text) > $nbrChar) {
$text = substr($text, 0, $nbrChar);
$text .= $append;
}
return $text;
}
我想幫助創建一個新功能來截斷電子郵件本地部分,類似於Google網上論壇中的內容。
abc...@gmail.com
這對使用Facebook代理電子郵件的用戶特別有用。
apps+2189712.12457.7b00f3c9e8bfabbeea8f73@proxymail.facebook.com
我想這個新函數會使用正則表達式找到@
然后將local-part截斷為一定數量的字符來生成類似的東西
apps+21...@proxymail.facebook.com
有任何建議如何解決這個問題?
謝謝!
此函數將截斷電子郵件的第一部分(如果找到@),如果找不到@則截斷其他字符串。
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...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.