Utils.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * Shaarli utilities
  4. */
  5. /**
  6. * Returns the small hash of a string, using RFC 4648 base64url format
  7. *
  8. * Small hashes:
  9. * - are unique (well, as unique as crc32, at last)
  10. * - are always 6 characters long.
  11. * - only use the following characters: a-z A-Z 0-9 - _ @
  12. * - are NOT cryptographically secure (they CAN be forged)
  13. *
  14. * In Shaarli, they are used as a tinyurl-like link to individual entries,
  15. * e.g. smallHash('20111006_131924') --> yZH23w
  16. */
  17. function smallHash($text)
  18. {
  19. $t = rtrim(base64_encode(hash('crc32', $text, true)), '=');
  20. return strtr($t, '+/', '-_');
  21. }
  22. /**
  23. * Tells if a string start with a substring
  24. */
  25. function startsWith($haystack, $needle, $case=true)
  26. {
  27. if ($case) {
  28. return (strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0);
  29. }
  30. return (strcasecmp(substr($haystack, 0, strlen($needle)), $needle) === 0);
  31. }
  32. /**
  33. * Tells if a string ends with a substring
  34. */
  35. function endsWith($haystack, $needle, $case=true)
  36. {
  37. if ($case) {
  38. return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0);
  39. }
  40. return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0);
  41. }
  42. ?>