handle_text.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package mokuai
  2. import (
  3. "fmt"
  4. "math"
  5. "math/rand"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "unicode"
  10. "unicode/utf8"
  11. )
  12. //文本_取中间
  13. func Text_GetBetween(str, start, end string) string {
  14. n := strings.Index(str, start)
  15. if n == -1 {
  16. n = 0
  17. } else {
  18. n = n + len(start) // 增加了else,不加的会把start带上
  19. }
  20. str = string([]byte(str)[n:])
  21. m := strings.Index(str, end)
  22. if m == -1 {
  23. m = len(str)
  24. }
  25. str = string([]byte(str)[:m])
  26. return str
  27. }
  28. //文本_寻找文本是否存在,不存在返回-1
  29. func Text_FindKeyword(str, keyword string) int {
  30. return strings.Index(str, keyword)
  31. }
  32. //取随机数 参数说明: [num 返回随机数的位数]
  33. func Text_GetRandNum(num int) string {
  34. //将时间戳设置成种子数
  35. rand.Seed(time.Now().UnixNano())
  36. //生成10个0-99之间的随机数
  37. str := ""
  38. for i := 0; i < num; i++ {
  39. str = str + Type_IntToString(rand.Intn(9))
  40. }
  41. return str
  42. }
  43. //取范围内随机数 参数说明: [min 最小值;max 最大值; 返回随机数的位数]
  44. func Text_GetRandNumInRange(min, max int64) int64 {
  45. rand.Seed(time.Now().UnixNano())
  46. if min >= max || min == 0 || max == 0 {
  47. return max
  48. }
  49. return rand.Int63n(max-min) + min
  50. }
  51. // 取字符串中汉字出现的数量
  52. func Text_GetCNWordsNum(str string) int {
  53. hzc := 0
  54. for _, v := range str {
  55. if unicode.Is(unicode.Han, v) {
  56. hzc++
  57. }
  58. }
  59. return hzc
  60. }
  61. // 取字符串个数
  62. func Text_GetStrNum(str string) int {
  63. return utf8.RuneCountInString(str)
  64. }
  65. // 以关键词分割文本
  66. func Text_GetSplitText(str, keywords string) []string {
  67. s1 := strings.Split(str, keywords)
  68. return s1
  69. }
  70. // 去除字符串首位空格
  71. func Text_DelOuterSpace(str string) string {
  72. return strings.TrimSpace(str)
  73. }
  74. // 文本替换 (文本,旧的准备替换掉的文本,新的要替换成的文本,替换多少次 n<0为全部替换 )
  75. func Text_Replace(str, old, new string, n int) string {
  76. return strings.Replace(str, old, new, n)
  77. }
  78. // 文本取左边 (取关键词左边的文本)
  79. func Text_GetLeftText(str, keywords string) string {
  80. comma := strings.Index(str, keywords)
  81. return str[:comma]
  82. }
  83. // 文本取右边 (取关键词右边的文本)
  84. func Text_GetRightText(str, keywords string) string {
  85. comma := strings.Index(str, keywords)
  86. return str[comma+len(keywords):]
  87. }
  88. // 删除关键词文本所在行 (待删除的文本,删除次数(-1为不限制,删除的包含关键词种类的次数) 。返回删除后的内容)
  89. func Text_DelKeywordline(str, keyword string, n int) string {
  90. linzu := strings.Split(str, "\n")
  91. lin := str
  92. n1 := 0
  93. for _, v := range linzu {
  94. if strings.Index(v, keyword) != -1 {
  95. lin = strings.ReplaceAll(lin, v+"\n", "")
  96. if n1 >= n {
  97. break
  98. }
  99. n1++
  100. }
  101. }
  102. return lin
  103. }
  104. // 数 四舍五入 (value 要整理的值,bit 保留几位小数)
  105. func Num_round45(value float64, bit int) float64 {
  106. n10 := math.Pow10(bit)
  107. v := math.Trunc((value+0.5/n10)*n10) / n10
  108. return v
  109. }
  110. /* 文本替换指定序数文本 。 如 一段文本内有3处 exit 0 , 那么可以指定仅替换 第二处的exit 0 ,其余不变。
  111. str : 原文本 ; old : 欲被替换的子文本1 ; new : 用作替换的子文本1 ; n : 指定替换的序数*/
  112. func Text_ReplaceNth(str, old, new string, n int) string {
  113. i := 0
  114. for m := 1; m <= n; m++ {
  115. x := strings.Index(str[i:], old)
  116. if x < 0 {
  117. break
  118. }
  119. i += x
  120. if m == n {
  121. return str[:i] + new + str[i+len(old):]
  122. }
  123. i += len(old)
  124. }
  125. return str
  126. }
  127. // 随机获取国内IP
  128. func Text_GetRandCNIp() string { // 国内部分IP
  129. rand.Seed(time.Now().UnixNano())
  130. ipzu := make(map[int]string)
  131. ipzu[1] = "607649792,608174079" //36.56.0.0-36.63.255.255
  132. ipzu[2] = "1038614528,1039007743" //106.80.0.0-106.95.255.255
  133. ipzu[3] = "1783627776,1784676351" //121.76.0.0-121.77.255.255
  134. ipzu[4] = "2078801920,2079064063" //123.232.0.0-123.235.255.255
  135. ipzu[5] = "-1950089216,-1948778497" //139.196.0.0-139.215.255.255
  136. ipzu[6] = "-1425539072,-1425014785" //171.8.0.0-171.15.255.255
  137. ipzu[7] = "-1236271104,-1235419137" //182.80.0.0-182.92.255.255
  138. ipzu[8] = "-770113536,-768606209" //210.25.0.0-210.47.255.255
  139. ipzu[9] = "-569376768,-564133889" //222.16.0.0-222.95.255.255
  140. rand_key := int(rand.Int63n(9-1) + 1)
  141. rand_ip := ipzu[rand_key]
  142. fmt.Println("rand_ip", rand_ip)
  143. comma := strings.Index(rand_ip, ",")
  144. min, _ := strconv.ParseInt(rand_ip[:comma], 10, 64)
  145. max, _ := strconv.ParseInt(rand_ip[comma+len(","):], 10, 64)
  146. fmt.Println("min", min, "max", max)
  147. if min >= max || min == 0 || max == 0 {
  148. return Net_IPNtoA(max)
  149. }
  150. rand.Seed(time.Now().UnixNano() + rand.Int63n(3))
  151. ip := Net_IPNtoA(rand.Int63n(max-min) + min)
  152. return ip
  153. //return long2ip(mt_rand($ip_long[$rand_key][0], $ip_long[$rand_key][1]));
  154. }
  155. // 随机获取IP ,可能包含内网IP
  156. func Text_GetRandIp() string {
  157. rand.Seed(time.Now().Unix())
  158. ip := fmt.Sprintf("%d.%d.%d.%d", rand.Intn(255), rand.Intn(255), rand.Intn(255), rand.Intn(255))
  159. return ip
  160. }