Home  >  Article  >  Backend Development  >  Implementing PHP's Addslashes and Stripslashes using Golang

Implementing PHP's Addslashes and Stripslashes using Golang

不言
不言Original
2018-04-14 16:40:423857browse

本篇文章给大家分享的内容是关于使用Golang实现PHP的Addslashes和Stripslashes ,有着一定的参考价值,有需要的朋友可以参考一下


// addslashes() 函数返回在预定义字符之前添加反斜杠的字符串。
// 预定义字符是:
// 单引号(')
// 双引号(")
// 反斜杠(\)
func Addslashes(str string) string {
	tmpRune := []rune{}
	strRune := []rune(str)
	for _, ch := range strRune {
		switch ch {
		case []rune{'\\'}[0], []rune{'"'}[0], []rune{'\''}[0]:
			tmpRune = append(tmpRune, []rune{'\\'}[0])
			tmpRune = append(tmpRune, ch)
		default:
			tmpRune = append(tmpRune, ch)
		}
	}
	return string(tmpRune)
}

// stripslashes() 函数删除由 addslashes() 函数添加的反斜杠。
func Stripslashes(str string) string {
	dstRune := []rune{}
	strRune := []rune(str)
	strLenth := len(strRune)
	for i := 0; i < strLenth; i++ {
		if strRune[i] == []rune{&#39;\\&#39;}[0] {
			i++
		}
		dstRune = append(dstRune, strRune[i])
	}
	return string(dstRune)
}

Github: https://github.com/wtmmac/webstrings


The above is the detailed content of Implementing PHP's Addslashes and Stripslashes using Golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn