public static function htmlentities($str = '', $encodeTags = true, $isXml = false, $charset = null)
    {
        $debug = \bdk\Debug::getInstance();
        $dbWas = $debug->setCfg('collect', false);
        $debug->groupCollapsed(__FUNCTION__);
        $return = '';
        $charsetsTry = array('UTF-8','ISO-8859-1');
        $debug->log('charset', $charset);
        if (is_null($charset) && extension_loaded('mbstring')) {
            $charset = mb_detect_encoding($str, mb_detect_order(), true);
            $debug->log('charset detected', $charset);
        }
        if ($charset) {
            $charset = self::getCharset($charset);
            if ($charset == 'UTF-8' && !Str::isUtf8($str)) {
                $charset = null;
            }
        }
        if ($charset) {
            #$debug->log('will try charset '.$charset.' first');
            array_unshift($charsetsTry, $charset);
            $charsetsTry = array_unique($charsetsTry);
        }
        $debug->log('charsetsTry', implode(', ', $charsetsTry));
        $return = self::htmlentitiesApply($str, $encodeTags, $isXml, $charsetsTry);
        #$debug->log('return', $return);
        $debug->groupEnd();
        $debug->setCfg('collect', $dbWas);
        return $return;
    }