[英]how to decode utf-8 in php from utf-8 python encode?
這是 python 中的代碼
值.py
print('Print 😀 Smile 🥇 2nd Best Private'.encode('utf-8'))
# b'Print \xf0\x9f\x98\x80 Smile \xf0\x9f\xa5\x87 2nd Best Private'
值.php
#get value from value.py
$bio=rtrim(ltrim($temp[2]));
$bio = preg_replace("/U\+([0-9a-f]{4,5})/mi", '&#x${1}', $bio);
echo ($bio);
echo($bio);
//output = \xf0\x9f\x98\x80 Smile \xf0\x9f\xa5\x87 2nd Best Private'
這是我天真的方法(在 Windows 中實現)。
腳本:
type .\SO\71805811.py
# -*- coding: utf-8 -*- print('Print Smile 2nd Best Private'.encode('utf-8'))
type .\SO\71805811.php
<?php var_dump($argn); $pattern = '(\\\\x(?=[0-9A-Fa-f]{2}))'; // ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ positive lookahead assertion $replacement = '='; $encoded = preg_replace($pattern, $replacement, $argn); var_dump(quoted_printable_decode($encoded)); ?>
Output :
.\SO\71805811.py
b'Print \xf0\x9f\x98\x80 Smile \xf0\x9f\xa5\x87 2nd Best Private'
.\SO\71805811.py | \php8\php.exe -F .\SO\71805811.php
string(65) "b'Print \xf0\x9f\x98\x80 Smile \xf0\x9f\xa5\x87 2nd Best Private'" string(41) "b'Print Smile 2nd Best Private'"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.