Browse Source

feat:增加方法

wangchuanjin 2 years ago
parent
commit
9d773593e7
1 changed files with 13 additions and 0 deletions
  1. 13 0
      common/common.go

+ 13 - 0
common/common.go

@@ -791,3 +791,16 @@ func IsWxBrowser(Request *http.Request) bool {
 		return false
 	}
 }
+
+//
+func ShortenTxt(length int, fixed, shorten string) string {
+	if len([]rune(fixed)) > length {
+		return ""
+	} else if len([]rune(fixed+shorten)) <= length {
+		return shorten
+	}
+	if limit := length - len([]rune(fixed)) - 3; limit >= 0 && limit <= len([]rune(shorten)) {
+		return string([]rune(shorten)[:limit]) + "..."
+	}
+	return ""
+}