public static function num_htmlentities($str)
    {
        $charset = 'ISO-8859-1';

        if (!self::$htmlentities) {
            self::$htmlentities = array();
            $table = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, $charset);
            foreach ($table as $char => $entity) {
                self::$htmlentities[$entity] = '&#' . ord($char) . ';';
            }
        }

        return str_replace(
            array_keys(
                self::$htmlentities
            ), self::$htmlentities, htmlentities($str, ENT_COMPAT | ENT_HTML401, $charset)
        );
    }