Search code examples
phpunicodehexhtml-entities

convert unicode to html entities hex


How to convert a Unicode string to HTML entities? (HEX not decimal)

For example, convert Français to Français.


Solution

  • Your string looks like UCS-4 encoding you can try

    $first = preg_replace_callback('/[\x{80}-\x{10FFFF}]/u', function ($m) {
        $char = current($m);
        $utf = iconv('UTF-8', 'UCS-4', $char);
        return sprintf("&#x%s;", ltrim(strtoupper(bin2hex($utf)), "0"));
    }, $string);
    

    Output

    string 'Français' (length=13)