hospital-open-api/utils/unserialize.go

34 lines
666 B
Go

package utils
import (
"errors"
"regexp"
"strconv"
)
// Unserialize 反序列化 PHP 序列化的字符串
func Unserialize(data string) (string, error) {
// 使用正则表达式找到字符串值
re := regexp.MustCompile(`s:(\d+):"(.*?)";`)
match := re.FindStringSubmatch(data)
if len(match) != 3 {
return "", errors.New("invalid input format")
}
// 解析字符串长度
length, err := strconv.Atoi(match[1])
if err != nil {
return "", err
}
// 解析字符串值
value := match[2]
// 检查字符串长度是否与值长度匹配
if len(value) != length {
return "", errors.New("string length does not match")
}
return value, nil
}