Back to Crypt class

Method safeStrlen

public static int
safeStrlen
(mixed $str)
Safely detect a string's length
Parameters
  • string $str String to check the length of
Returns
  • int
Since
  • 3.5
-
  • mbstring.func_overload
  • \RuntimeException
Class: Crypt
Project: Joomla

Method safeStrlen - Source code

/**
 * Safely detect a string's length
 *
 * This method is derived from \ParagonIE\Halite\Util::safeStrlen()
 *
 * @param   string  $str  String to check the length of
 *
 * @return  integer
 *
 * @since   3.5
 * @ref     mbstring.func_overload
 * @throws  \RuntimeException
 */
public static function safeStrlen($str)
{
    static $exists = null;
    if ($exists === null) {
        $exists = \function_exists('mb_strlen');
    }
    if ($exists) {
        $length = mb_strlen($str, '8bit');
        if ($length === false) {
            throw new \RuntimeException('mb_strlen() failed unexpectedly');
        }
        return $length;
    }
    // If we reached here, we can rely on strlen to count bytes:
    return \strlen($str);
}