简体   繁体   English

将PHP字符串转换为1个UTF-8字符

[英]Convert PHP String to 1 UTF-8 character

$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

How can I encode my $input string to get \\xF0 also as one character ? 如何将$ input字符串编码为\\ xF0也作为一个字符? Thank you very much ! 非常感谢你 !

<?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;

prints a 😚 in my firefox 在我的Firefox中打印一个😚

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM