@param int $value
@param int $length

@return string
554663
private function intToMaxLengthHex($value, $length)
    {
        return str_pad(substr(dechex($value), 0, $length), $length, '0', STR_PAD_LEFT);
    }
46097
public static function randomString(int $length): string
    {
        $characters = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789';
        $string = '';
        for ($i = 0; $i < $length; $i++) {
            $string .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $string;
    }
686260
public static function random(int $length): string
    {
        $byteLength = ($length < 10) ? 10 : $length;
        $chars = base64_encode(random_bytes($byteLength));
        $chars = str_replace(["+", "/", "="], "", $chars);
        return substr($chars, 0, $length);
    }
1232624
public static function GenerateRandomString(int $length = 20)
    {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }
667935
public static function string($value, $max_length = null)
    {
        /*if (!is_string($value)) {
            throw new \Exception('not_a_string');
        }*/
        $value = filter_var(trim($value), FILTER_SANITIZE_STRING);

        if ($max_length && mb_strlen($value) > $max_length) {
            $value = mb_substr($value, 0, $max_length);
        }
        return $value;
    }
192900
public static function randomString(int $length): string
    {
        $random = '';
        $passes = 0;

        do {
            foreach (str_split(openssl_random_pseudo_bytes($length * 3)) as $c) {
                $ascii = ord($c);

                if ($ascii >= 48 && $ascii <= 57 || $ascii >= 65 && $ascii <= 90 || $ascii >= 97 && $ascii <= 122) {
                    $random .= chr($ascii);
                }
            }
        } while (strlen($random) < $length && $passes++ < 50);

        if ($passes == 50) {
            throw new Exception('Unable to generate random string even after a lot passes... something is really wrong!');
        }

        return substr($random, 0, $length);
    }
1344317
protected function encodeInteger($value, $length) {
        $format = "%'.0" . $length . "d";
        $output = null === $value ? "" : sprintf($format, $value);
        if ($length < strlen($output)) {
            throw new TooLongDataException($value, $length);
        }
        return $output;
    }
1186387
protected function fill( $value, $length )
	{
		if( $length && $value != "*" )
		{
			if( strlen( $value ) < $length )
			{
				$diff	= $length - strlen( $value );
				for( $i=0; $i<$diff; $i++ )
					$value	= "0".$value;
			}
		}
		return $value;
	}
1033400
public function read(int $length = 8192): string
    {
        return iconv($this->charset, 'UTF-8', $this->inputStream->read($length));
    }
1083447
static public function EncodeLength($length)
    {
        self::CheckMessageLength($length);

        $string = "";
        do{
            $digit = $length % 0x80;
            $length = $length >> 7;
            // if there are more digits to encode, set the top bit of this digit
            if ( $length > 0 ) $digit = ($digit | 0x80);
            $string .= chr($digit);
        } while ( $length > 0 );

        return $string;
    }
728272
private function random(int $length = 32): string
    {
        try {
            if (empty($string = random_bytes($length))) {
                throw new \RuntimeException("Unable to generate random string");
            }
        } catch (\Throwable $e) {
            throw new \RuntimeException("Unable to generate random string", $e->getCode(), $e);
        }

        return substr(base64_encode($string), 0, $length);
    }
375011
public static function randAlphabet($length)
    {
        if ($length < 0) $length = 1;
        if ($length > 64) $length = 64;
        $code = [];
        for ($i = 0; $i < $length; $i++) {
            $code[$i] = self::$alphaCodeSet[mt_rand(0, strlen(self::$alphaCodeSet) - 1)];
        }
        return implode("", $code);
    }
277422
private static function preserveBreakpoint($value, $length)
    {
        if (strlen($value) <= $length) {
            return $length;
        }
        $breakpoint = mb_strpos($value, ' ', $length);
        $length = $breakpoint;
        if (false === $breakpoint) {
            $length = mb_strlen($value);
        }
        return $length;
    }
342782
public function generateNonceStr($length = 32)
    {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $str = '';
        for ($i = 0; $i < $length; ++$i) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }

        return $str;
    }
230838
public function tget(string $column, int $length=32) {
		// we get the raw value, truncate it, and escape it afterwards
		return strlen($this->get($column, true)) > $length ? 
			Format::htmlSafe(
				Format::truncate(
					$this->get($column, true),
					$length
				)
			) : 
			$this->get($column);
	}
1106661
public static function generateRandom($length = 10)
    {
        $max = ceil($length / 40);
        $random = '';
        for ($i = 0; $i < $max; $i++) {
            $random .= sha1(microtime(true) . mt_rand(10000, 90000));
        }

        return substr($random, 0, $length);
    }
916765
final protected function fwrite($string, $length = null)
	{
		return is_int($length)
			? fwrite($this->fhandle, $string, $length)
			: fwrite($this->fhandle, $string);
	}