如何从 utf-8 python 编码解码 php 中的 utf-8?

how to decode utf-8 in php from utf-8 python encode?

这是python

中的代码

value.py

print('Print  Smile  2nd Best Private'.encode('utf-8'))
# b'Print \xf0\x9f\x98\x80 Smile \xf0\x9f\xa5\x87 2nd Best Private'

value.php

#get value from value.py
$bio=rtrim(ltrim($temp[2]));
$bio = preg_replace("/U\+([0-9a-f]{4,5})/mi", '&#x', $bio);
echo ($bio);

echo($bio);
//output = \xf0\x9f\x98\x80 Smile \xf0\x9f\xa5\x87 2nd Best Private'

这是我的幼稚方法(在Windows中实现)。

脚本:

type .\SO805811.py
# -*- coding: utf-8 -*-

print('Print  Smile  2nd Best Private'.encode('utf-8'))
type .\SO805811.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));
?>

输出:

.\SO805811.py
b'Print \xf0\x9f\x98\x80 Smile \xf0\x9f\xa5\x87 2nd Best Private'
.\SO805811.py | \php8\php.exe -F .\SO805811.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'"