PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Utility/StringUtility.php

https://github.com/brightmarch/BrightmarchUtilityBundle
PHP | 48 lines | 27 code | 8 blank | 13 comment | 1 complexity | 78c8ed8f8db626663e989d034240ad18 MD5 | raw file
  1. <?php
  2. namespace Brightmarch\Bundle\UtilityBundle\Utility;
  3. class StringUtility
  4. {
  5. public function __construct()
  6. {
  7. }
  8. /**
  9. * Creates a random string of a specified length.
  10. *
  11. * @param integer $length
  12. * @return string
  13. */
  14. public function randomString($length=32)
  15. {
  16. $length = abs((int)$length);
  17. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  18. $pool_len = strlen($pool)-1;
  19. $random = '';
  20. for ($i=0; $i<$length; $i++) {
  21. $random .= $pool[mt_rand(0, $pool_len)];
  22. }
  23. return $random;
  24. }
  25. /**
  26. * Generates a 4th generation UUID.
  27. * Shamelessly taken from:
  28. * http://us2.php.net/manual/en/function.uniqid.php#69164
  29. *
  30. * @return string
  31. */
  32. public function uuid()
  33. {
  34. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  35. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  36. mt_rand(0, 0x0fff) | 0x4000,
  37. mt_rand(0, 0x3fff) | 0x8000,
  38. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
  39. }
  40. }