StringUtil.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace Curl;
  3. class StringUtil
  4. {
  5. public static function characterReversePosition($haystack, $needle, $part = false)
  6. {
  7. if (function_exists('\mb_strrchr')) {
  8. return \mb_strrchr($haystack, $needle, $part);
  9. } else {
  10. return \strrchr($haystack, $needle);
  11. }
  12. }
  13. public static function length($string)
  14. {
  15. if (function_exists('\mb_strlen')) {
  16. return \mb_strlen($string);
  17. } else {
  18. return \strlen($string);
  19. }
  20. }
  21. public static function position($haystack, $needle, $offset = 0)
  22. {
  23. if (function_exists('\mb_strpos')) {
  24. return \mb_strpos($haystack, $needle, $offset);
  25. } else {
  26. return \strpos($haystack, $needle, $offset);
  27. }
  28. }
  29. public static function reversePosition($haystack, $needle, $offset = 0)
  30. {
  31. if (function_exists('\mb_strrpos')) {
  32. return \mb_strrpos($haystack, $needle, $offset);
  33. } else {
  34. return \strrpos($haystack, $needle, $offset);
  35. }
  36. }
  37. /**
  38. * Return true when $haystack starts with $needle.
  39. *
  40. * @access public
  41. * @param $haystack
  42. * @param $needle
  43. *
  44. * @return bool
  45. */
  46. public static function startsWith($haystack, $needle)
  47. {
  48. return self::substring($haystack, 0, self::length($needle)) === $needle;
  49. }
  50. public static function substring($string, $start, $length)
  51. {
  52. if (function_exists('\mb_substr')) {
  53. return \mb_substr($string, $start, $length);
  54. } else {
  55. return \substr($string, $start, $length);
  56. }
  57. }
  58. }