Converting named HTML entities to numeric HTML entities

前端 未结 6 1868
半阙折子戏
半阙折子戏 2020-12-08 08:18

Is there a PHP function to convert named HTML entities into their respective numeric HTML entities?

For example:

$str = \"Oggi è un bel&am         


        
6条回答
  •  天涯浪人
    2020-12-08 08:38

    This solution is based on the code from php.net:

    function entities_to_unicode($str) {
        $str = html_entity_decode($str, ENT_QUOTES, 'UTF-8');
        $str = preg_replace_callback("/(&#[0-9]+;)/", function($m) { return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); }, $str);
        return $str;
    }
    
    $str = 'Oggi è un bel giorno';
    echo entities_to_unicode($str);
    

提交回复
热议问题