convert unicode to html entities hex

前端 未结 4 2098
日久生厌
日久生厌 2020-12-03 19:27

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

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

4条回答
  •  不思量自难忘°
    2020-12-03 20:03

    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)
    

提交回复
热议问题