StringUtils.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @author Takashi Nojima
  4. * @copyright Copyright 2014, Takashi Nojima
  5. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
  6. */
  7. namespace App\Util\Lexer;
  8. /**
  9. * String utility.
  10. *
  11. * @author Takashi Nojima
  12. * @copyright Copyright 2014, Takashi Nojima
  13. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
  14. */
  15. class StringUtils
  16. {
  17. /**
  18. * alias of mb_substr.
  19. *
  20. * @param string $str
  21. * @param int $start
  22. * @param int $length
  23. * @param string $encoding
  24. *
  25. * @return string
  26. */
  27. public static function substr($str, $start, $length = null, $encoding = 'UTF-8')
  28. {
  29. if (is_null($length)) {
  30. // for PHP <= 5.4.7
  31. $length = mb_strlen($str, $encoding);
  32. }
  33. return mb_substr($str, $start, $length, $encoding);
  34. }
  35. /**
  36. * alias of mb_strlen.
  37. *
  38. * @param string $str
  39. * @param string $encoding
  40. *
  41. * @return int
  42. */
  43. public static function strlen($str, $encoding = 'UTF-8')
  44. {
  45. return mb_strlen($str, $encoding);
  46. }
  47. /**
  48. * alias of mb_strpos.
  49. *
  50. * @param string $haystack
  51. * @param string $needle
  52. * @param int $offset
  53. * @param string $encoding
  54. *
  55. * @return int
  56. */
  57. public static function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-8')
  58. {
  59. return mb_strpos($haystack, $needle, $offset, $encoding);
  60. }
  61. /**
  62. * A multibyte-aware substring replacement function.
  63. *
  64. * @param string $string The string to modify.
  65. * @param string $replacement The replacement string.
  66. * @param int $start The start of the replacement.
  67. * @param int $length The number of characters to replace.
  68. * @param string $encoding The encoding of the string.
  69. *
  70. * @return string The modified string.
  71. *
  72. * @see http://www.php.net/manual/en/function.substr-replace.php#90146
  73. */
  74. public static function substrReplace($string, $replacement, $start, $length = null, $encoding = 'UTF-8')
  75. {
  76. if (extension_loaded('mbstring') === true) {
  77. $string_length = static::strlen($string, $encoding);
  78. if ($start < 0) {
  79. $start = max(0, $string_length + $start);
  80. } elseif ($start > $string_length) {
  81. $start = $string_length;
  82. }
  83. if ($length < 0) {
  84. $length = max(0, $string_length - $start + $length);
  85. } elseif ((is_null($length) === true) || ($length > $string_length)) {
  86. $length = $string_length;
  87. }
  88. if (($start + $length) > $string_length) {
  89. $length = $string_length - $start;
  90. }
  91. $suffixOffset = $start + $length;
  92. $suffixLength = $string_length - $start - $length;
  93. return static::substr($string, 0, $start, $encoding).$replacement.static::substr($string, $suffixOffset, $suffixLength, $encoding);
  94. }
  95. return (is_null($length) === true) ? substr_replace($string, $replacement, $start) : substr_replace($string, $replacement, $start, $length);
  96. }
  97. }