Implementing PHP's Addslashes and Stripslashes using Golang

不言
Release: 2023-03-23 21:08:01
Original
3843 people have browsed it

本篇文章给大家分享的内容是关于使用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)
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!