diff --git a/map.go b/map.go index 91434b9..435f465 100644 --- a/map.go +++ b/map.go @@ -2,6 +2,7 @@ package helper import ( "errors" + "net/url" "sort" "strings" ) @@ -119,3 +120,29 @@ func HttpBuildQuery(data map[string]string) string { return strings.Trim(query, "&") } + +/** + * 对map字典按照顺序进行拼接,并且把每个value进行类urlencode + * @param data map[string]string + * @2021/10/14 + * @wyy + */ +func HttpBuildStringQuery(data map[string]string) string { + + keySlice := []string{} + + for k, _ := range data { + keySlice = append(keySlice, k) + } + // sort.Strings(keySlice) //排序字符串 + + var query string + + for _, value := range keySlice { + if value != "" && data[value] != "" { + query += StringJoin(value, "=", url.QueryEscape(data[value]), "&") + } + } + + return strings.Trim(query, "&") +} diff --git a/string.go b/string.go index 1ef416e..8588724 100755 --- a/string.go +++ b/string.go @@ -204,6 +204,26 @@ func IsInStringArray(arr []string, str string) bool { return isIn } +/** + * 字符串在字符切片中对应索引 + */ +func InStringArrayIndex(arr []string, str string) int { + // var isIn bool = false + length := len(arr) + if length < 1 { + return -1 + } + var index int = -1 + for k, item := range arr { + if item == str { + // isIn = true + index = k + break + } + } + return index +} + /* * 删除多余空格 * 2019/05/05 @@ -251,3 +271,27 @@ func UrlJoin(host, url string) string { return StringJoin(host, url) } + + +/** + * 去除字符串的html标签 + * @2021/10/20 + */ +func TrimHtml(src string) string { + //将HTML标签全转换成小写 + re, _ := regexp.Compile("\\<[\\S\\s]+?\\>") + src = re.ReplaceAllStringFunc(src, strings.ToLower) + //去除STYLE + re, _ = regexp.Compile("\\") + src = re.ReplaceAllString(src, "") + //去除SCRIPT + re, _ = regexp.Compile("\\") + src = re.ReplaceAllString(src, "") + //去除所有尖括号内的HTML代码,并换成换行符 + re, _ = regexp.Compile("\\<[\\S\\s]+?\\>") + src = re.ReplaceAllString(src, "\n") + //去除连续的换行符 + re, _ = regexp.Compile("\\s{2,}") + src = re.ReplaceAllString(src, "\n") + return strings.TrimSpace(src) +}