Helper.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace AlibabaCloud\Tea;
  3. class Helper
  4. {
  5. /**
  6. * @param string $content
  7. * @param string $prefix
  8. * @param string $end
  9. * @param string[] $filter
  10. *
  11. * @return string|string[]
  12. */
  13. public static function findFromString($content, $prefix, $end, $filter = ['"', ' '])
  14. {
  15. $len = mb_strlen($prefix);
  16. $pos = mb_strpos($content, $prefix);
  17. if (false === $pos) {
  18. return '';
  19. }
  20. $pos_end = mb_strpos($content, $end, $pos);
  21. $str = mb_substr($content, $pos + $len, $pos_end - $pos - $len);
  22. return str_replace($filter, '', $str);
  23. }
  24. /**
  25. * @param string $str
  26. *
  27. * @return bool
  28. */
  29. public static function isJson($str)
  30. {
  31. json_decode($str);
  32. return \JSON_ERROR_NONE == json_last_error();
  33. }
  34. /**
  35. * @param mixed $value
  36. *
  37. * @return bool
  38. */
  39. public static function isBytes($value)
  40. {
  41. if (!\is_array($value)) {
  42. return false;
  43. }
  44. $i = 0;
  45. foreach ($value as $k => $ord) {
  46. if ($k !== $i) {
  47. return false;
  48. }
  49. if (!\is_int($ord)) {
  50. return false;
  51. }
  52. if ($ord < 0 || $ord > 255) {
  53. return false;
  54. }
  55. ++$i;
  56. }
  57. return true;
  58. }
  59. /**
  60. * Convert a bytes to string(utf8).
  61. *
  62. * @param array $bytes
  63. *
  64. * @return string the return string
  65. */
  66. public static function toString($bytes)
  67. {
  68. $str = '';
  69. foreach ($bytes as $ch) {
  70. $str .= \chr($ch);
  71. }
  72. return $str;
  73. }
  74. /**
  75. * @return array
  76. */
  77. public static function merge(array $arrays)
  78. {
  79. $result = [];
  80. foreach ($arrays as $array) {
  81. foreach ($array as $key => $value) {
  82. if (\is_int($key)) {
  83. $result[] = $value;
  84. continue;
  85. }
  86. if (isset($result[$key]) && \is_array($result[$key])) {
  87. $result[$key] = self::merge(
  88. [$result[$key], $value]
  89. );
  90. continue;
  91. }
  92. $result[$key] = $value;
  93. }
  94. }
  95. return $result;
  96. }
  97. }