123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- package mokuai
- import (
- "fmt"
- "math"
- "math/rand"
- "strconv"
- "strings"
- "time"
- "unicode"
- "unicode/utf8"
- )
- //文本_取中间
- func Text_GetBetween(str, start, end string) string {
- n := strings.Index(str, start)
- if n == -1 {
- n = 0
- } else {
- n = n + len(start) // 增加了else,不加的会把start带上
- }
- str = string([]byte(str)[n:])
- m := strings.Index(str, end)
- if m == -1 {
- m = len(str)
- }
- str = string([]byte(str)[:m])
- return str
- }
- //文本_寻找文本是否存在,不存在返回-1
- func Text_FindKeyword(str, keyword string) int {
- return strings.Index(str, keyword)
- }
- //取随机数 参数说明: [num 返回随机数的位数]
- func Text_GetRandNum(num int) string {
- //将时间戳设置成种子数
- rand.Seed(time.Now().UnixNano())
- //生成10个0-99之间的随机数
- str := ""
- for i := 0; i < num; i++ {
- str = str + Type_IntToString(rand.Intn(9))
- }
- return str
- }
- //取范围内随机数 参数说明: [min 最小值;max 最大值; 返回随机数的位数]
- func Text_GetRandNumInRange(min, max int64) int64 {
- rand.Seed(time.Now().UnixNano())
- if min >= max || min == 0 || max == 0 {
- return max
- }
- return rand.Int63n(max-min) + min
- }
- // 取字符串中汉字出现的数量
- func Text_GetCNWordsNum(str string) int {
- hzc := 0
- for _, v := range str {
- if unicode.Is(unicode.Han, v) {
- hzc++
- }
- }
- return hzc
- }
- // 取字符串个数
- func Text_GetStrNum(str string) int {
- return utf8.RuneCountInString(str)
- }
- // 以关键词分割文本
- func Text_GetSplitText(str, keywords string) []string {
- s1 := strings.Split(str, keywords)
- return s1
- }
- // 去除字符串首位空格
- func Text_DelOuterSpace(str string) string {
- return strings.TrimSpace(str)
- }
- // 文本替换 (文本,旧的准备替换掉的文本,新的要替换成的文本,替换多少次 n<0为全部替换 )
- func Text_Replace(str, old, new string, n int) string {
- return strings.Replace(str, old, new, n)
- }
- // 文本取左边 (取关键词左边的文本)
- func Text_GetLeftText(str, keywords string) string {
- comma := strings.Index(str, keywords)
- return str[:comma]
- }
- // 文本取右边 (取关键词右边的文本)
- func Text_GetRightText(str, keywords string) string {
- comma := strings.Index(str, keywords)
- return str[comma+len(keywords):]
- }
- // 删除关键词文本所在行 (待删除的文本,删除次数(-1为不限制,删除的包含关键词种类的次数) 。返回删除后的内容)
- func Text_DelKeywordline(str, keyword string, n int) string {
- linzu := strings.Split(str, "\n")
- lin := str
- n1 := 0
- for _, v := range linzu {
- if strings.Index(v, keyword) != -1 {
- lin = strings.ReplaceAll(lin, v+"\n", "")
- if n1 >= n {
- break
- }
- n1++
- }
- }
- return lin
- }
- // 数 四舍五入 (value 要整理的值,bit 保留几位小数)
- func Num_round45(value float64, bit int) float64 {
- n10 := math.Pow10(bit)
- v := math.Trunc((value+0.5/n10)*n10) / n10
- return v
- }
- /* 文本替换指定序数文本 。 如 一段文本内有3处 exit 0 , 那么可以指定仅替换 第二处的exit 0 ,其余不变。
- str : 原文本 ; old : 欲被替换的子文本1 ; new : 用作替换的子文本1 ; n : 指定替换的序数*/
- func Text_ReplaceNth(str, old, new string, n int) string {
- i := 0
- for m := 1; m <= n; m++ {
- x := strings.Index(str[i:], old)
- if x < 0 {
- break
- }
- i += x
- if m == n {
- return str[:i] + new + str[i+len(old):]
- }
- i += len(old)
- }
- return str
- }
- // 随机获取国内IP
- func Text_GetRandCNIp() string { // 国内部分IP
- rand.Seed(time.Now().UnixNano())
- ipzu := make(map[int]string)
- ipzu[1] = "607649792,608174079" //36.56.0.0-36.63.255.255
- ipzu[2] = "1038614528,1039007743" //106.80.0.0-106.95.255.255
- ipzu[3] = "1783627776,1784676351" //121.76.0.0-121.77.255.255
- ipzu[4] = "2078801920,2079064063" //123.232.0.0-123.235.255.255
- ipzu[5] = "-1950089216,-1948778497" //139.196.0.0-139.215.255.255
- ipzu[6] = "-1425539072,-1425014785" //171.8.0.0-171.15.255.255
- ipzu[7] = "-1236271104,-1235419137" //182.80.0.0-182.92.255.255
- ipzu[8] = "-770113536,-768606209" //210.25.0.0-210.47.255.255
- ipzu[9] = "-569376768,-564133889" //222.16.0.0-222.95.255.255
- rand_key := int(rand.Int63n(9-1) + 1)
- rand_ip := ipzu[rand_key]
- fmt.Println("rand_ip", rand_ip)
- comma := strings.Index(rand_ip, ",")
- min, _ := strconv.ParseInt(rand_ip[:comma], 10, 64)
- max, _ := strconv.ParseInt(rand_ip[comma+len(","):], 10, 64)
- fmt.Println("min", min, "max", max)
- if min >= max || min == 0 || max == 0 {
- return Net_IPNtoA(max)
- }
- rand.Seed(time.Now().UnixNano() + rand.Int63n(3))
- ip := Net_IPNtoA(rand.Int63n(max-min) + min)
- return ip
- //return long2ip(mt_rand($ip_long[$rand_key][0], $ip_long[$rand_key][1]));
- }
- // 随机获取IP ,可能包含内网IP
- func Text_GetRandIp() string {
- rand.Seed(time.Now().Unix())
- ip := fmt.Sprintf("%d.%d.%d.%d", rand.Intn(255), rand.Intn(255), rand.Intn(255), rand.Intn(255))
- return ip
- }
|