Browse Source

Merge branch 'master' of ssh://git.tetele.net:4001/tgo/helper

master v0.2.2
guzeng 3 years ago
parent
commit
0eb2d1bac9
2 changed files with 71 additions and 0 deletions
  1. +27
    -0
      map.go
  2. +44
    -0
      string.go

+ 27
- 0
map.go View File

@ -2,6 +2,7 @@ package helper
import ( import (
"errors" "errors"
"net/url"
"sort" "sort"
"strings" "strings"
) )
@ -119,3 +120,29 @@ func HttpBuildQuery(data map[string]string) string {
return strings.Trim(query, "&") 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, "&")
}

+ 44
- 0
string.go View File

@ -204,6 +204,26 @@ func IsInStringArray(arr []string, str string) bool {
return isIn 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 * 2019/05/05
@ -251,3 +271,27 @@ func UrlJoin(host, url string) string {
return StringJoin(host, url) 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("\\<style[\\S\\s]+?\\</style\\>")
src = re.ReplaceAllString(src, "")
//去除SCRIPT
re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>")
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)
}

Loading…
Cancel
Save