Encode string data between PHP and Python
What would be the best way or a simple and good way to encode string data in python for later reading in php?
开发者_如何学编程Failed examples:
1: In Python:
>>> encoded = u'test ö'.encode('utf-8')
'text \xc3\xb6'
In PHP:
>>> $decoded = utf8_decode('test \xc3\xb6');
test \xc3\xb6
???
2: In Python:
>>> encoded = u'test ö'.encode('latin-1')
'test \xf6'
In PHP:
$decoded = ???;
You have to use double quotes to get backslash escape sequences parsed in PHP.
# php -r 'echo utf8_decode("test \xc3\xb6");'
test ö
And using UTF8 is certainly one of the most interoperable ways to pass string data between programming languages / systems / etc.
精彩评论