$caption = "Hello World \xF0"; // \xF0 is evaluated as one character
$input = $_POST["mes"]; // \xF0 is stored as four characters
// equivalent to:
$input = 'Hello World \xF0'; // \xF0 also stored as four characters
如何编码我的$ input字符串以将\ xF0也作为一个字符? 非常感谢你!
答案 0 :(得分:2)
<?php
$data = '\xF0\x9F\x98\x9A';
$pattern = '!
\\\ # a literal backslash
x # followed by a literal x
( # capture the following {
[0-9a-fA-F] # any hexadecimal digit
{2} # actually two of them
) # }
!x';
// replace all \xnn by "the byte nn"
$data = preg_replace_callback(
$pattern,
function($captures) {
return chr( hexdec($captures[1]) );
},
$data
);
// $data contains now the byte sequence given by the hexadecimal notation
// but keep in mind that php3,4,5's core has no notion of character encoding
// a string is just a sequence of single bytes...
// I'm using a browser to display the result
// it must know that the output stream is utf-8 encoded
// default_charset will send the appropriate http response header for that
ini_set('default_charset', 'utf-8');
echo $data;
在我的firefox中打印一个