helpers_string.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. if (!function_exists('__e')) {
  3. /**
  4. * Encodes a string for HTML entities.
  5. *
  6. * @param string $str The string to encode.
  7. * @param int $flags Flags for htmlentities.
  8. * @param string $encoding The character encoding.
  9. * @return string The encoded string.
  10. */
  11. function __e(string $str, int $flags = ENT_QUOTES, string $encoding = 'UTF-8'): string
  12. {
  13. return htmlentities($str, $flags, $encoding);
  14. }
  15. }
  16. if (!function_exists('str_starts_with')) {
  17. /**
  18. * @param string $haystack
  19. * @param string $needle
  20. * @return bool
  21. */
  22. function str_starts_with(string $haystack, string $needle): bool
  23. {
  24. return substr($haystack, 0, strlen($needle)) === $needle;
  25. }
  26. }
  27. if (!function_exists('str_ends_with')) {
  28. /**
  29. * @param string $haystack
  30. * @param string $needle
  31. * @return bool
  32. */
  33. function str_ends_with(string $haystack, string $needle): bool
  34. {
  35. return substr($haystack, -strlen($needle)) === $needle;
  36. }
  37. }
  38. if (!function_exists('str_contains')) {
  39. /**
  40. * @param string $haystack
  41. * @param string $needle
  42. * @return bool
  43. */
  44. function str_contains(string $haystack, string $needle): bool
  45. {
  46. return strpos($haystack, $needle) !== false;
  47. }
  48. }
  49. if (!function_exists('human_readable_bytes')) {
  50. function human_readable_bytes(int $size, int $precision = 2): string
  51. {
  52. if ($size <= 0) {
  53. return '0 B';
  54. }
  55. $base = log($size, 1024);
  56. $suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
  57. $class = (int) floor($base);
  58. return round(pow(1024, $base - $class), $precision) . ' ' . $suffixes[$class];
  59. }
  60. }
  61. if (!function_exists('_m_convert')) {
  62. /**
  63. * @deprecated Use human_readable_bytes instead
  64. */
  65. function _m_convert($size): string
  66. {
  67. return human_readable_bytes((int) $size);
  68. }
  69. }