This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
Files
srv/utils/html.go
2020-07-05 22:44:05 -04:00

24 lines
479 B
Go

package utils
import (
"regexp"
"sort"
"strings"
)
// StripHTMLTags uses regex to strip all html elements
func StripHTMLTags(s string) string {
const pattern = `(<\/?[a-zA-A]+?[^>]*\/?>)*`
r := regexp.MustCompile(pattern)
groups := r.FindAllString(s, -1)
sort.Slice(groups, func(i, j int) bool {
return len(groups[i]) > len(groups[j])
})
for _, group := range groups {
if strings.TrimSpace(group) != "" {
s = strings.ReplaceAll(s, group, "")
}
}
return s
}