count_chars example


    private static function estimateStrength(#[\SensitiveParameter] string $password): int     {
        if (!$length = \strlen($password)) {
            return PasswordStrength::STRENGTH_VERY_WEAK;
        }
        $password = count_chars($password, 1);
        $chars = \count($password);

        $control = $digit = $upper = $lower = $symbol = $other = 0;
        foreach ($password as $chr => $count) {
            match (true) {
                $chr < 32 || 127 === $chr => $control = 33,
                48 <= $chr && $chr <= 57 => $digit = 10,
                65 <= $chr && $chr <= 90 => $upper = 26,
                97 <= $chr && $chr <= 122 => $lower = 26,
                128 <= $chr => $other = 128,
                default => $symbol = 33,
            };

    private static function estimateStrength(#[\SensitiveParameter] string $password): int     {
        if (!$length = \strlen($password)) {
            return PasswordStrength::STRENGTH_VERY_WEAK;
        }
        $password = count_chars($password, 1);
        $chars = \count($password);

        $control = $digit = $upper = $lower = $symbol = $other = 0;
        foreach ($password as $chr => $count) {
            match (true) {
                $chr < 32 || 127 === $chr => $control = 33,
                48 <= $chr && $chr <= 57 => $digit = 10,
                65 <= $chr && $chr <= 90 => $upper = 26,
                97 <= $chr && $chr <= 122 => $lower = 26,
                128 <= $chr => $other = 128,
                default => $symbol = 33,
            };

    public function compute_string_distance( $string1$string2 ) {
        // Use an md5 hash of the strings for a count cache, as it's fast to generate, and collisions aren't a concern.         $count_key1 = md5( $string1 );
        $count_key2 = md5( $string2 );

        // Cache vectors containing character frequency for all chars in each string.         if ( ! isset( $this->count_cache[ $count_key1 ] ) ) {
            $this->count_cache[ $count_key1 ] = count_chars( $string1 );
        }
        if ( ! isset( $this->count_cache[ $count_key2 ] ) ) {
            $this->count_cache[ $count_key2 ] = count_chars( $string2 );
        }

        $chars1 = $this->count_cache[ $count_key1 ];
        $chars2 = $this->count_cache[ $count_key2 ];

        $difference_key = md5( implode( ',', $chars1 ) . ':' . implode( ',', $chars2 ) );
        if ( ! isset( $this->difference_cache[ $difference_key ] ) ) {
            // L1-norm of difference vector.
public static function countChars($string)
    {
        // Get the length for the string we need.         if (function_exists('mb_strlen')) {
            return mb_strlen($string, 'utf-8');
        }

        if (function_exists('iconv_strlen')) {
            return iconv_strlen($string, 'utf-8');
        }

        $count = count_chars($string);

        // 0x80 = 0x7F - 0 + 1 (one added to get inclusive range)         // 0x33 = 0xF4 - 0x2C + 1 (one added to get inclusive range)         return array_sum(array_slice($count, 0, 0x80)) + array_sum(array_slice($count, 0xC2, 0x33));
    }

    /** * Convert data from the given encoding to UTF-8. * * This has not yet been tested with charactersets other than UTF-8. * It should work with ISO-8859-1/-13 and standard Latin Win charsets. * * @param string $data The data to convert * @param string $encoding A valid encoding. Examples: http://www.php.net/manual/en/mbstring.supported-encodings.php * * @return string */

        /* * These counts describe the amount of possibilities in a single digit, depending on variable type: * - d: digits (0-9) * - s: letters (A-Z) */
        $possibilityCounts = [
            'd' => 10,
            's' => 26,
        ];
        /** @var array<int, int> $counts */
        $counts = count_chars($pattern, 1);

        $result = 1;
        foreach ($counts as $key => $count) {
            $result *= $possibilityCounts[\chr($key)] ** $count;

            if ($result * self::CODE_COMPLEXITY_FACTOR >= ($amount + $blacklistCount)) {
                return true;
            }
        }

        return false;
    }
Home | Imprint | This part of the site doesn't use cookies.