简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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