Johnny2312
Johnny2312

Reputation: 3

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 !

Upvotes: 0

Views: 724

Answers (1)

VolkerK
VolkerK

Reputation: 96189

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

Upvotes: 2

Related Questions