在PHP和Python之间编码字符串数据
在Python中,有什么简单又好的方法可以把字符串数据编码,以便以后在PHP中读取呢?
失败的例子:
1: 在Python中:
>>> encoded = u'test ö'.encode('utf-8')
'text \xc3\xb6'
在PHP中:
>>> $decoded = utf8_decode('test \xc3\xb6');
test \xc3\xb6
???
2: 在Python中:
>>> encoded = u'test ö'.encode('latin-1')
'test \xf6'
在PHP中:
$decoded = ???;
1 个回答
3
在PHP中,你必须使用双引号,这样才能让反斜杠转义序列被正确解析。
# php -r 'echo utf8_decode("test \xc3\xb6");'
test ö
而使用UTF8编码绝对是不同编程语言、系统之间传递字符串数据最通用的方法之一。