| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- <?php
- namespace Songshenzong\Support\Traits;
- use stdClass;
- use SimpleXMLElement;
- /**
- * Trait Str
- *
- * @package Songshenzong\Support\Traits
- */
- trait Str
- {
- /**
- * @param string $string
- *
- * @return array
- */
- public static function toArray($string = '')
- {
- if ($string === '') {
- return [];
- }
- if (self::isJson($string)) {
- return json_decode($string, true);
- }
- if (self::isXml($string)) {
- return self::xmlToArray($string);
- }
- $unserialize = self::unserialize($string);
- if ($unserialize === false) {
- return [];
- }
- if (\is_array($unserialize)) {
- return $unserialize;
- }
- return [];
- }
- /**
- * @param string $string
- *
- * @return bool
- */
- public static function isJson($string = '')
- {
- if ($string === '') {
- return false;
- }
- \json_decode($string, true);
- if (\json_last_error()) {
- return false;
- }
- return true;
- }
- /**
- * @param string $string
- *
- * @return bool
- */
- public static function isXml($string = '')
- {
- if ($string === '') {
- return false;
- }
- $xml_parser = xml_parser_create();
- if (!xml_parse($xml_parser, $string, true)) {
- xml_parser_free($xml_parser);
- return false;
- }
- return true;
- }
- /**
- * @param string $string
- *
- * @return array
- */
- public static function xmlToArray($string)
- {
- return json_decode(json_encode(simplexml_load_string($string)), true);
- }
- /**
- * @param string $serialized
- *
- * @return mixed
- */
- public static function unserialize($serialized)
- {
- // Set Handle
- set_error_handler(
- function () {
- },
- E_ALL
- );
- $result = unserialize((string)$serialized);
- // Restores the previous error handler function
- restore_error_handler();
- if ($result === false) {
- return false;
- }
- return $result;
- }
- /**
- * @param string $string
- *
- * @return stdClass|SimpleXMLElement
- *
- */
- public static function toObject($string = '')
- {
- if ($string === '') {
- return new stdClass();
- }
- if (self::isJson($string)) {
- return json_decode($string);
- }
- if (self::isXml($string)) {
- return self::xmlToObject($string);
- }
- $unserialize = self::unserialize($string);
- if ($unserialize === false) {
- return new stdClass();
- }
- if (\is_object($unserialize)) {
- return $unserialize;
- }
- return new stdClass();
- }
- /**
- * @param string $string
- *
- * @return SimpleXMLElement
- */
- public static function xmlToObject($string)
- {
- return simplexml_load_string($string);
- }
- /**
- * @param string $string
- *
- * @return bool
- */
- public static function isSerialized($string)
- {
- // Set Handle
- set_error_handler(
- function () {
- },
- E_ALL
- );
- $result = unserialize($string);
- // Restores the previous error handler function
- restore_error_handler();
- return !($result === false);
- }
- /**
- * @param string $string
- *
- * @return string
- */
- public static function filter($string)
- {
- $filter = [
- "\n",
- '`',
- '·',
- '~',
- '!',
- '!',
- '@',
- '#',
- '$',
- '¥',
- '%',
- '^',
- '……',
- '&',
- '*',
- '(',
- ')',
- '(',
- ')',
- '-',
- '_',
- '——',
- '+',
- '=',
- '|',
- '\\',
- '[',
- ']',
- '【',
- '】',
- '{',
- '}',
- ';',
- ';',
- ':',
- ':',
- '\'',
- '"',
- '“',
- '”',
- ',',
- ',',
- '<',
- '>',
- '《',
- '》',
- '.',
- '。',
- '/',
- '、',
- '?',
- '?',
- ';',
- 'nbsp',
- ];
- $str = str_replace($filter, '', $string);
- return trim($str);
- }
- /**
- * @param string $string
- *
- * @return string
- */
- public static function trim($string)
- {
- $filter = [
- "\0",
- "\n",
- "\t",
- "\x0B",
- "\r",
- ' ',
- ];
- $str = str_replace($filter, '', $string);
- return trim($str);
- }
- /**
- * Is Set and Not Empty.
- *
- * @param $value
- *
- * @return bool
- */
- public static function isSetAndNotEmpty($value)
- {
- return isset($value) && !empty($value);
- }
- /**
- * Is Set and Not Empty and Not Null.
- *
- * @param $value
- *
- * @return bool
- */
- public static function isSetAndNotEmptyAndNotNull($value)
- {
- return isset($value) && !empty($value) && $value !== 'null';
- }
- }
|