Util.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * The Mixer strategy interface.
  4. *
  5. * All mixing strategies must implement this interface
  6. *
  7. * PHP version 5.3
  8. *
  9. * @category PHPPasswordLib
  10. * @package Hash
  11. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  12. * @copyright 2011 The Authors
  13. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  14. * @version Build @@version@@
  15. */
  16. namespace SecurityLib;
  17. /**
  18. * The Utility trait.
  19. *
  20. * Contains methods used internally to this library.
  21. *
  22. * @category PHPPasswordLib
  23. * @package Random
  24. * @author Scott Arciszewski <scott@arciszewski.me>
  25. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  26. * @codeCoverageIgnore
  27. */
  28. abstract class Util {
  29. /**
  30. * Return the length of a string, even in the presence of
  31. * mbstring.func_overload
  32. *
  33. * @param string $string the string we're measuring
  34. * @return int
  35. */
  36. public static function safeStrlen($string)
  37. {
  38. if (\function_exists('mb_strlen')) {
  39. return \mb_strlen($string, '8bit');
  40. }
  41. return \strlen($string);
  42. }
  43. /**
  44. * Return a string contained within a string, even in the presence of
  45. * mbstring.func_overload
  46. *
  47. * @param string $string The string we're searching
  48. * @param int $start What offset should we begin
  49. * @param int|null $length How long should the substring be?
  50. * (default: the remainder)
  51. * @return string
  52. */
  53. public static function safeSubstr($string, $start = 0, $length = null)
  54. {
  55. if (\function_exists('mb_substr')) {
  56. return \mb_substr($string, $start, $length, '8bit');
  57. } elseif ($length !== null) {
  58. return \substr($string, $start, $length);
  59. }
  60. return \substr($string, $start);
  61. }
  62. }