htmlspecialchars

htmlspecialchars

func htmlspecialchars(str string) string {
	//将HTML标签全转换成小写
	re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
	str = re.ReplaceAllStringFunc(str, strings.ToLower)
	//去除STYLE
	re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>")
	str = re.ReplaceAllString(str, "")
	//去除SCRIPT
	re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>")
	str = re.ReplaceAllString(str, "")
	//去除所有尖括号内的HTML代码
	re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
	str = re.ReplaceAllString(str, "")
	//去除换行符<br>
	re, _ = regexp.Compile("\\s{1,}")
	str = re.ReplaceAllString(str, "")
	//去除换行符\n
	str = strings.Replace(str, "\\n", "", -1)
	return strings.TrimSpace(str)
}