Str.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. namespace Songshenzong\Support\Traits;
  3. use stdClass;
  4. use SimpleXMLElement;
  5. /**
  6. * Trait Str
  7. *
  8. * @package Songshenzong\Support\Traits
  9. */
  10. trait Str
  11. {
  12. /**
  13. * @param string $string
  14. *
  15. * @return array
  16. */
  17. public static function toArray($string = '')
  18. {
  19. if ($string === '') {
  20. return [];
  21. }
  22. if (self::isJson($string)) {
  23. return json_decode($string, true);
  24. }
  25. if (self::isXml($string)) {
  26. return self::xmlToArray($string);
  27. }
  28. $unserialize = self::unserialize($string);
  29. if ($unserialize === false) {
  30. return [];
  31. }
  32. if (\is_array($unserialize)) {
  33. return $unserialize;
  34. }
  35. return [];
  36. }
  37. /**
  38. * @param string $string
  39. *
  40. * @return bool
  41. */
  42. public static function isJson($string = '')
  43. {
  44. if ($string === '') {
  45. return false;
  46. }
  47. \json_decode($string, true);
  48. if (\json_last_error()) {
  49. return false;
  50. }
  51. return true;
  52. }
  53. /**
  54. * @param string $string
  55. *
  56. * @return bool
  57. */
  58. public static function isXml($string = '')
  59. {
  60. if ($string === '') {
  61. return false;
  62. }
  63. $xml_parser = xml_parser_create();
  64. if (!xml_parse($xml_parser, $string, true)) {
  65. xml_parser_free($xml_parser);
  66. return false;
  67. }
  68. return true;
  69. }
  70. /**
  71. * @param string $string
  72. *
  73. * @return array
  74. */
  75. public static function xmlToArray($string)
  76. {
  77. return json_decode(json_encode(simplexml_load_string($string)), true);
  78. }
  79. /**
  80. * @param string $serialized
  81. *
  82. * @return mixed
  83. */
  84. public static function unserialize($serialized)
  85. {
  86. // Set Handle
  87. set_error_handler(
  88. function () {
  89. },
  90. E_ALL
  91. );
  92. $result = unserialize((string)$serialized);
  93. // Restores the previous error handler function
  94. restore_error_handler();
  95. if ($result === false) {
  96. return false;
  97. }
  98. return $result;
  99. }
  100. /**
  101. * @param string $string
  102. *
  103. * @return stdClass|SimpleXMLElement
  104. *
  105. */
  106. public static function toObject($string = '')
  107. {
  108. if ($string === '') {
  109. return new stdClass();
  110. }
  111. if (self::isJson($string)) {
  112. return json_decode($string);
  113. }
  114. if (self::isXml($string)) {
  115. return self::xmlToObject($string);
  116. }
  117. $unserialize = self::unserialize($string);
  118. if ($unserialize === false) {
  119. return new stdClass();
  120. }
  121. if (\is_object($unserialize)) {
  122. return $unserialize;
  123. }
  124. return new stdClass();
  125. }
  126. /**
  127. * @param string $string
  128. *
  129. * @return SimpleXMLElement
  130. */
  131. public static function xmlToObject($string)
  132. {
  133. return simplexml_load_string($string);
  134. }
  135. /**
  136. * @param string $string
  137. *
  138. * @return bool
  139. */
  140. public static function isSerialized($string)
  141. {
  142. // Set Handle
  143. set_error_handler(
  144. function () {
  145. },
  146. E_ALL
  147. );
  148. $result = unserialize($string);
  149. // Restores the previous error handler function
  150. restore_error_handler();
  151. return !($result === false);
  152. }
  153. /**
  154. * @param string $string
  155. *
  156. * @return string
  157. */
  158. public static function filter($string)
  159. {
  160. $filter = [
  161. "\n",
  162. '`',
  163. '·',
  164. '~',
  165. '!',
  166. '!',
  167. '@',
  168. '#',
  169. '$',
  170. '¥',
  171. '%',
  172. '^',
  173. '……',
  174. '&',
  175. '*',
  176. '(',
  177. ')',
  178. '(',
  179. ')',
  180. '-',
  181. '_',
  182. '——',
  183. '+',
  184. '=',
  185. '|',
  186. '\\',
  187. '[',
  188. ']',
  189. '【',
  190. '】',
  191. '{',
  192. '}',
  193. ';',
  194. ';',
  195. ':',
  196. ':',
  197. '\'',
  198. '"',
  199. '“',
  200. '”',
  201. ',',
  202. ',',
  203. '<',
  204. '>',
  205. '《',
  206. '》',
  207. '.',
  208. '。',
  209. '/',
  210. '、',
  211. '?',
  212. '?',
  213. ';',
  214. 'nbsp',
  215. ];
  216. $str = str_replace($filter, '', $string);
  217. return trim($str);
  218. }
  219. /**
  220. * @param string $string
  221. *
  222. * @return string
  223. */
  224. public static function trim($string)
  225. {
  226. $filter = [
  227. "\0",
  228. "\n",
  229. "\t",
  230. "\x0B",
  231. "\r",
  232. ' ',
  233. ];
  234. $str = str_replace($filter, '', $string);
  235. return trim($str);
  236. }
  237. /**
  238. * Is Set and Not Empty.
  239. *
  240. * @param $value
  241. *
  242. * @return bool
  243. */
  244. public static function isSetAndNotEmpty($value)
  245. {
  246. return isset($value) && !empty($value);
  247. }
  248. /**
  249. * Is Set and Not Empty and Not Null.
  250. *
  251. * @param $value
  252. *
  253. * @return bool
  254. */
  255. public static function isSetAndNotEmptyAndNotNull($value)
  256. {
  257. return isset($value) && !empty($value) && $value !== 'null';
  258. }
  259. }