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

This the code in 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${1}', $bio);
echo ($bio);

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


Solution 1:[1]

Here's my naive approach (implemented in Windows).

Scripts:

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'"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1