src/String/Entropy.php
public function bitsPerCharacter($string)
    {
        if (is_string($string) === false) {
            throw new \InvalidArgumentException('Argument must be a string.');
        }
        $sum = 0;
        $length = strlen($string);
        foreach (array_values(array_count_values(str_split($string))) as $count) {
            $freq = $count / $length;
            $sum += $freq * log($freq, 2);
        }
        return (int) ceil(- $sum);
    }
public function bitsPerCharacter($string)
    {
        if (is_string($string) === false) {
            throw new \InvalidArgumentException('Argument must be a string.');
        }
        $sum = 0;
        $length = strlen($string);
        foreach (array_values(array_count_values(str_split($string))) as $count) {
            $freq = $count / $length;
            $sum += $freq * log($freq, 2);
        }
        return (int) ceil(- $sum);
    }
public function bitsPerCharacter ( $ string ) { if ( is_string ( $ string ) === false ) { throw new \ InvalidArgumentException ( 'Argument must be a string.' ) ; } $ sum = 0 ; $ length = strlen ( $ string ) ; foreach ( array_values ( array_count_values ( str_split ( $ string ) ) ) as $ count ) { $ freq = $ count / $ length ; $ sum += $ freq * log ( $ freq , 2 ) ; } return ( int ) ceil ( - $ sum ) ; }
Calculate the number of bits needed to encode a single character of the given string.

@param string $string The string to calculate the entropy of.

@return int The number of bits of entropy needed to encode a single character of the given string.
Calculate the number of bits needed to encode a single character of the given string .