改名
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/4 0004
|
||||
* Time: 14:57
|
||||
*/
|
||||
|
||||
namespace Snowflake\Core;
|
||||
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class ArrayAccess
|
||||
* @package Snowflake\Core
|
||||
*/
|
||||
class ArrayAccess
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function toArray($data)
|
||||
{
|
||||
if (!is_object($data) && !is_array($data)) {
|
||||
return $data;
|
||||
}
|
||||
if (is_object($data)) {
|
||||
$data = self::objToArray($data);
|
||||
}
|
||||
$tmp = [];
|
||||
if (!is_array($data)) {
|
||||
return $data;
|
||||
}
|
||||
foreach ($data as $key => $val) {
|
||||
if (is_array($val) || is_object($val)) {
|
||||
$tmp[$key] = self::toArray($val);
|
||||
} else {
|
||||
$tmp[$key] = $val;
|
||||
}
|
||||
}
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return array|mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function objToArray($data)
|
||||
{
|
||||
if (!is_object($data)) {
|
||||
return $data;
|
||||
}
|
||||
if (method_exists($data, 'get')) {
|
||||
$data = $data->get();
|
||||
if (is_array($data)) {
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
if (method_exists($data, 'toArray')) {
|
||||
$data = $data->toArray();
|
||||
} else {
|
||||
$data = get_object_vars((object)$data);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $oldArray
|
||||
* @param array $newArray
|
||||
* @return array
|
||||
*/
|
||||
public static function merge(array $oldArray, array $newArray)
|
||||
{
|
||||
if (empty($oldArray)) {
|
||||
return $newArray;
|
||||
} else if (empty($newArray)) {
|
||||
return $oldArray;
|
||||
}
|
||||
foreach ($newArray as $item => $value) {
|
||||
if (!isset($oldArray[$item])) {
|
||||
$oldArray[$item] = $value;
|
||||
}
|
||||
if (is_array($value)) {
|
||||
$oldArray[$item] = self::merge($oldArray[$item], $value);
|
||||
} else {
|
||||
$oldArray[$item] = $value;
|
||||
}
|
||||
}
|
||||
return $oldArray;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: dell
|
||||
* Date: 2019/1/14 0014
|
||||
* Time: 13:50
|
||||
*/
|
||||
|
||||
namespace Snowflake\Core;
|
||||
|
||||
/**
|
||||
* Class DateFormat
|
||||
* @package Snowflake\Snowflake\Core
|
||||
*/
|
||||
class DateFormat
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $time
|
||||
* @return bool|false|int|string
|
||||
*/
|
||||
private static function check($time)
|
||||
{
|
||||
if ($time === null) {
|
||||
$time = time();
|
||||
} else if (is_numeric($time)) {
|
||||
$length = strlen(floatval($time));
|
||||
if ($length != 10 && $length != 13) {
|
||||
return false;
|
||||
}
|
||||
} else if (is_string($time)) {
|
||||
$time = strtotime($time);
|
||||
}
|
||||
|
||||
if (date('Y-m-d', $time)) {
|
||||
return $time;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param null $time
|
||||
* @return bool|false|int
|
||||
*
|
||||
* 获取指定日期当周第一天的时间
|
||||
*/
|
||||
public static function getWeekCurrentDay($time = null)
|
||||
{
|
||||
if (!($time = static::check($time))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$time = strtotime('-' . (date('N') - 1) . 'days', $time);
|
||||
|
||||
return strtotime(date('Y-m-d'), $time);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param null $time
|
||||
* @return bool|false|int
|
||||
*
|
||||
* 获取指定日期当月第一天的时间
|
||||
*/
|
||||
public static function getMonthCurrentDay($time = null)
|
||||
{
|
||||
if (!($time = static::check($time))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return strtotime(date('Y-m', $time) . '-01');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $time
|
||||
* @return bool|false|int|string
|
||||
* 指定的月份有几天
|
||||
*/
|
||||
public static function getMonthTotalDay($time)
|
||||
{
|
||||
if (!($time = static::check($time))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$time = date('t', $time);
|
||||
|
||||
return $time;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $startTime
|
||||
* @param null $endTime
|
||||
* @return string
|
||||
*/
|
||||
public static function mtime($startTime, $endTime = null)
|
||||
{
|
||||
if ($endTime === null) {
|
||||
$endTime = microtime(true);
|
||||
}
|
||||
return sprintf('%.7f', $endTime - $startTime);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Core;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Snowflake\Abstracts\Component;
|
||||
|
||||
/**
|
||||
* Class Dtl
|
||||
* @package Snowflake\Core
|
||||
*/
|
||||
class Dtl extends Component
|
||||
{
|
||||
|
||||
protected $params;
|
||||
|
||||
|
||||
/**
|
||||
* Dtl constructor.
|
||||
* @param $params
|
||||
*/
|
||||
public function __construct($params)
|
||||
{
|
||||
parent::__construct([]);
|
||||
$this->params = $params;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
if (!is_array($this->params)) {
|
||||
return ArrayAccess::toArray($this->params);
|
||||
}
|
||||
return $this->params;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed|null
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
$array = $this->toArray();
|
||||
if (!isset($array[$name])) {
|
||||
return null;
|
||||
}
|
||||
return $array[$name];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Core;
|
||||
|
||||
|
||||
/**
|
||||
* Class Help
|
||||
* @package Snowflake\Snowflake\Core
|
||||
*/
|
||||
class Help
|
||||
{
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public static function toXml(array $data)
|
||||
{
|
||||
$xml = "<xml>";
|
||||
foreach ($data as $key => $val) {
|
||||
if (is_numeric($val)) {
|
||||
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
|
||||
} else {
|
||||
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
|
||||
}
|
||||
}
|
||||
$xml .= "</xml>";
|
||||
return $xml;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $xml
|
||||
* @return array|mixed
|
||||
*/
|
||||
public static function toArray($xml)
|
||||
{
|
||||
if (empty($xml)) {
|
||||
return [];
|
||||
} else if (is_array($xml)) {
|
||||
return $xml;
|
||||
}
|
||||
if (!Xml::isXml($xml)) {
|
||||
$xml = JSON::decode($xml);
|
||||
}
|
||||
return $xml;
|
||||
|
||||
/* $matchQuote = '/(<\?xml.*?\?>)?<([a-zA-Z_]+)>(<([a-zA-Z_]+)><!.*?><\/\4>)+<\/\2>/';*/
|
||||
// if (!preg_match($matchQuote, $xml)) {
|
||||
// return self::jsonToArray($xml);
|
||||
// }
|
||||
// try {
|
||||
// $data = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
|
||||
// if ($data !== false) {
|
||||
// $data = json_decode(json_encode($data), TRUE);
|
||||
// } else {
|
||||
// $data = $xml;
|
||||
// }
|
||||
// } catch (\Exception $exception) {
|
||||
// $data = $xml;
|
||||
// } finally {
|
||||
// return $data;
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
public static function jsonToArray($xml)
|
||||
{
|
||||
$_xml = json_decode($xml, true);
|
||||
if (is_null($_xml)) {
|
||||
return $xml;
|
||||
}
|
||||
return $_xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $xml
|
||||
* @return mixed
|
||||
*/
|
||||
public static function xmlToArray($xml)
|
||||
{
|
||||
if (is_array($xml)) {
|
||||
return $xml;
|
||||
}
|
||||
if (($data = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)) !== false) {
|
||||
return json_decode(json_encode($data), TRUE);
|
||||
}
|
||||
if (!is_null($json = json_decode($xml, TRUE))) {
|
||||
return $json;
|
||||
}
|
||||
return $xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $parameter
|
||||
* @return array|false|string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function toString($parameter)
|
||||
{
|
||||
if (!is_string($parameter)) {
|
||||
$parameter = ArrayAccess::toArray($parameter);
|
||||
if (is_array($parameter)) {
|
||||
$parameter = JSON::encode($parameter);
|
||||
}
|
||||
}
|
||||
return $parameter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $json
|
||||
* @return false|mixed|string
|
||||
*/
|
||||
public static function toJson($json)
|
||||
{
|
||||
if (is_object($json)) {
|
||||
$json = get_object_vars($json);
|
||||
}
|
||||
if (is_array($json)) {
|
||||
return json_encode($json, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
$matchQuote = '/(<\?xml.*?\?>)?<([a-zA-Z_]+)>(<([a-zA-Z_]+)><!.*?><\/\4>)+<\/\2>/';
|
||||
if (preg_match($matchQuote, $json)) {
|
||||
$json = self::xmlToArray($json);
|
||||
} else {
|
||||
$json = json_decode($json, true);
|
||||
}
|
||||
if (!is_array($json)) {
|
||||
$json = [];
|
||||
}
|
||||
return json_encode($json, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $length
|
||||
* @return string
|
||||
*
|
||||
* 随机字符串
|
||||
*/
|
||||
public static function random($length = 20)
|
||||
{
|
||||
$res = [];
|
||||
$str = 'abcdefghijklmnopqrstuvwxyz';
|
||||
$str .= strtoupper($str) . '1234567890';
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$rand = substr($str, rand(0, strlen($str) - 2), 1);
|
||||
if (empty($rand)) {
|
||||
$rand = substr($str, strlen($str) - 3, 1);
|
||||
}
|
||||
array_push($res, $rand);
|
||||
}
|
||||
|
||||
return implode($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
* @param $key
|
||||
* @param $type
|
||||
* @return string
|
||||
*/
|
||||
public static function sign(array $array, $key, $type)
|
||||
{
|
||||
ksort($array, SORT_ASC);
|
||||
$string = [];
|
||||
foreach ($array as $hashKey => $val) {
|
||||
if (empty($val)) {
|
||||
continue;
|
||||
}
|
||||
$string[] = $hashKey . '=' . $val;
|
||||
}
|
||||
$string[] = 'key=' . $key;
|
||||
$string = implode('&', $string);
|
||||
if ($type == 'MD5') {
|
||||
return strtoupper(md5($string));
|
||||
} else {
|
||||
return hash('sha256', $string);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: admin
|
||||
* Date: 2019-03-20
|
||||
* Time: 01:04
|
||||
*/
|
||||
|
||||
namespace Snowflake\Core;
|
||||
|
||||
/**
|
||||
* Class JSON
|
||||
* @package Snowflake\Snowflake\Core
|
||||
*/
|
||||
class JSON
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return false|string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function encode($data)
|
||||
{
|
||||
if (empty($data)) {
|
||||
return $data;
|
||||
}
|
||||
if (is_array($data)) {
|
||||
return self::filter(ArrayAccess::toArray($data));
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
private static function filter($data)
|
||||
{
|
||||
array_walk_recursive($data, function (&$value, $key) {
|
||||
if (!is_numeric($value)) {
|
||||
return;
|
||||
}
|
||||
if (is_int(+$value)) {
|
||||
$value = +$value;
|
||||
}
|
||||
});
|
||||
return json_encode($data, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @param bool $asArray
|
||||
* @return mixed
|
||||
*/
|
||||
public static function decode($data, $asArray = true)
|
||||
{
|
||||
if (is_array($data)) {
|
||||
return $data;
|
||||
}
|
||||
return json_decode($data, $asArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $code
|
||||
* @param string $message
|
||||
* @param array $data
|
||||
* @param int $count
|
||||
* @param array $exPageInfo
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
public static function to($code, $message = '', $data = [], $count = 0, $exPageInfo = [])
|
||||
{
|
||||
$params['code'] = $code;
|
||||
if (!is_string($message)) {
|
||||
$params['param'] = $message;
|
||||
if (!empty($data)) {
|
||||
$params['exPageInfo'] = $data;
|
||||
}
|
||||
$params['message'] = 'System success.';
|
||||
} else {
|
||||
$params['message'] = $message;
|
||||
$params['param'] = $data;
|
||||
}
|
||||
if (!empty($exPageInfo)) {
|
||||
$params['exPageInfo'] = $exPageInfo;
|
||||
}
|
||||
$params['count'] = $count;
|
||||
if (is_numeric($data) || !is_numeric($count)) {
|
||||
$params['count'] = $data;
|
||||
$params['exPageInfo'] = $count;
|
||||
}
|
||||
if ((int)$params['count'] == -100) {
|
||||
$params['count'] = 1;
|
||||
}
|
||||
|
||||
ksort($params, SORT_ASC);
|
||||
|
||||
return static::encode($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $state
|
||||
* @param $body
|
||||
* @return false|int|string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function output($state, $body)
|
||||
{
|
||||
$params['state'] = $state;
|
||||
$params['body'] = ArrayAccess::toArray($body);
|
||||
|
||||
return static::encode($params);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Core;
|
||||
|
||||
/**
|
||||
* Class Reader
|
||||
* @package Snowflake\Snowflake\Core
|
||||
*/
|
||||
class Reader
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param $filepath
|
||||
* @param int $page
|
||||
* @param int $size
|
||||
* @return array and int
|
||||
*/
|
||||
public static function readerServerLogPagination($filepath, $page = 1, $size = 20)
|
||||
{
|
||||
$count = 0;
|
||||
$strings = [];
|
||||
$offset = ($page - 1) * $size;
|
||||
|
||||
if (!file_exists($filepath)) {
|
||||
return [0, []];
|
||||
}
|
||||
|
||||
//只读方式打开文件
|
||||
$fp = fopen($filepath, "r");
|
||||
|
||||
//开始循环读取$buffer_size
|
||||
while (!feof($fp)) {
|
||||
//读文件到缓冲区
|
||||
$buffer = fgets($fp);
|
||||
$count++;
|
||||
if ($count > $offset && count($strings) < $size) {
|
||||
$strings[] = [
|
||||
'id' => $count,
|
||||
'content' => $buffer
|
||||
];
|
||||
}
|
||||
}
|
||||
//关闭文件
|
||||
fclose($fp);
|
||||
unset($fp);
|
||||
|
||||
return ['total' => $count, 'list' => $strings];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $filename
|
||||
* @param $start
|
||||
* @param $lines
|
||||
* @return mixed
|
||||
*/
|
||||
public static function read_backward_line($filename, $start, $lines)
|
||||
{
|
||||
$lines++;
|
||||
$offset = -1;
|
||||
$read = '';
|
||||
$fp = @fopen($filename, "r");
|
||||
|
||||
$tmpStart = 0;
|
||||
while ($lines && fseek($fp, $offset, SEEK_END) >= 0) {
|
||||
|
||||
$c = fgetc($fp);
|
||||
if ($c == "\n" || $c == "\r") {
|
||||
if (++$tmpStart >= $start)
|
||||
$lines--;
|
||||
}
|
||||
|
||||
|
||||
if ($tmpStart >= $start)
|
||||
$read .= $c;
|
||||
$offset--;
|
||||
}
|
||||
|
||||
$read = trim($read);
|
||||
|
||||
$contents = [];
|
||||
$read = array_reverse(explode("\n", strrev($read)));
|
||||
foreach ($read as $key => $value) {
|
||||
if (empty($value)) {
|
||||
unset($read[$key]);
|
||||
} else {
|
||||
$contents[] = ['content' => $value, 'id' => $key + $start];
|
||||
}
|
||||
}
|
||||
|
||||
$response['total'] = self::read_count_by_file($filename);
|
||||
$response['list'] = $contents;
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $filepath
|
||||
* @return int
|
||||
*/
|
||||
private static function read_count_by_file($filepath)
|
||||
{
|
||||
$count = 0;
|
||||
//只读方式打开文件
|
||||
$fp = fopen($filepath, "r");
|
||||
|
||||
//开始循环读取$buffer_size
|
||||
while (fgets($fp)) {
|
||||
$count++;
|
||||
}
|
||||
//关闭文件
|
||||
fclose($fp);
|
||||
unset($fp);
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $filepath
|
||||
* @param int $page
|
||||
* @param int $size
|
||||
* @return array
|
||||
*/
|
||||
public static function folderPagination($filepath, $page = 1, $size = 20)
|
||||
{
|
||||
$count = 0;
|
||||
$strings = [];
|
||||
$offset = ($page - 1) * $size;
|
||||
|
||||
if (!is_dir($filepath)) {
|
||||
return [0, []];
|
||||
}
|
||||
|
||||
foreach (glob($filepath . '/*') as $key => $value) {
|
||||
$count++;
|
||||
if ($key < $offset || count($strings) >= $size) {
|
||||
continue;
|
||||
}
|
||||
$explode = explode(DIRECTORY_SEPARATOR, $value);
|
||||
|
||||
$addTime = fileatime($value);
|
||||
$changeTime = filectime($value);
|
||||
$modifyTime = filemtime($value);
|
||||
$strings[] = [
|
||||
'id' => $count,
|
||||
'path' => $value,
|
||||
'isDir' => (int)is_dir($value),
|
||||
'name' => end($explode),
|
||||
'atime' => [
|
||||
'format' => date('Y-m-d H:i:s', $addTime),
|
||||
'microtime' => $addTime
|
||||
],
|
||||
'ctime' => [
|
||||
'format' => date('Y-m-d H:i:s', $changeTime),
|
||||
'microtime' => $changeTime
|
||||
],
|
||||
'mtime' => [
|
||||
'format' => date('Y-m-d H:i:s', $modifyTime),
|
||||
'microtime' => $modifyTime
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
array_multisort($strings, array_column($strings, 'isDir'), SORT_DESC);
|
||||
|
||||
return ['total' => $count, 'list' => $strings];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
<?php
|
||||
|
||||
namespace Snowflake\Core;
|
||||
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class Str
|
||||
* @package Snowflake\Snowflake\Core
|
||||
*/
|
||||
class Str
|
||||
{
|
||||
|
||||
const STRING = 'abcdefghijklmnopqrstuvwxyz';
|
||||
|
||||
const NUMBER = '01234567890';
|
||||
|
||||
/**
|
||||
* @param int $length
|
||||
*
|
||||
* @return string
|
||||
* 获取随机字符串
|
||||
*/
|
||||
public static function rand(int $length = 20)
|
||||
{
|
||||
$string = '';
|
||||
if ($length < 1) $length = 20;
|
||||
$default = self::STRING . strtoupper(self::STRING) . self::NUMBER;
|
||||
$default = str_split($default);
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$string .= $default[array_rand($default)];
|
||||
}
|
||||
return (string)$string;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $length
|
||||
*
|
||||
* @return int
|
||||
* 获取随机数字
|
||||
*/
|
||||
public static function random(int $length = 20)
|
||||
{
|
||||
$number = '';
|
||||
$default = str_split(self::NUMBER);
|
||||
if ($length < 1) $length = 1;
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$number .= $default[array_rand($default)];
|
||||
}
|
||||
return $number;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $string
|
||||
* @param $sublen
|
||||
* @param bool $strip_tags
|
||||
* @param string $append
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function cut_str_utf8($string, $sublen, $strip_tags = true, $append = '...')
|
||||
{
|
||||
if ($strip_tags) {
|
||||
$string = strip_tags($string);
|
||||
}//去掉签标
|
||||
$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
|
||||
preg_match_all($pa, $string, $t_string);
|
||||
$str = "";
|
||||
for ($i = 0; $i < count($t_string[0]); $i++) {
|
||||
$str .= $t_string[0][$i];
|
||||
//转为gbk,一个汉字长度为2
|
||||
if (strlen(@iconv('utf-8', 'gbk', $str)) >= $sublen) {
|
||||
if ($i != count($t_string[0]) - 1) $str .= $append;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @param null $callback
|
||||
* @return bool
|
||||
* 判断是否为json字符串
|
||||
*/
|
||||
public static function isJson($data, $callback = null)
|
||||
{
|
||||
$json = !is_null(json_decode($data)) && !is_numeric($data);
|
||||
if ($json && is_callable($callback, true)) {
|
||||
return call_user_func($callback, $data);
|
||||
}
|
||||
return $json;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @param null $callBack
|
||||
* @return bool
|
||||
* 判断是否序列化字符串
|
||||
*/
|
||||
public static function isSerialize($data, $callBack = null)
|
||||
{
|
||||
$false = !empty($data) && unserialize($data) !== false;
|
||||
if ($false && is_callable($callBack, true)) {
|
||||
return call_user_func($callBack, $data);
|
||||
}
|
||||
return $false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $string
|
||||
* @param int $length
|
||||
*
|
||||
* @param string $append
|
||||
* @return string
|
||||
*/
|
||||
public static function cut($string, int $length = 20, $append = '...')
|
||||
{
|
||||
if (empty($string)) {
|
||||
return '';
|
||||
}
|
||||
if ($length < 1) {
|
||||
$length = 1;
|
||||
}
|
||||
$array = str_split($string);
|
||||
if (count($array) <= $length) {
|
||||
return implode('', $array);
|
||||
}
|
||||
$string = implode('', array_slice($array, 0, $length));
|
||||
if (!empty($append)) {
|
||||
$string .= $append;
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $str
|
||||
* @param int $number
|
||||
* @param string $key
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function encrypt($str, $number = 10, $key = 'xshucai.com')
|
||||
{
|
||||
$res = [];
|
||||
$add = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||
$len = strlen($key) < 0 ? 1 : strlen($key) + 5 > strlen($add) ? strlen($add) - 5 : strlen($key);
|
||||
if ($number < 1) $number = 10;
|
||||
$array = str_split($str);
|
||||
asort($array);
|
||||
$str = implode('', $array);
|
||||
for ($i = 0; $i < $number; $i++) {
|
||||
$_tmp = md5($key) . md5($str) . mb_substr($add, $len, $len + 5, 'utf-8');
|
||||
$res[] = md5($_tmp);
|
||||
}
|
||||
sort($res, SORT_STRING);
|
||||
return hash('sha384', implode('', $res));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
* @param $type
|
||||
* @return string
|
||||
*/
|
||||
public static function filename($file, $type)
|
||||
{
|
||||
switch ($type) {
|
||||
case 'image/png':
|
||||
return md5_file($file) . '.png';
|
||||
case 'image/jpeg':
|
||||
case 'image/jpg':
|
||||
return md5_file($file) . '.jpg';
|
||||
case 'image/gif':
|
||||
return md5_file($file) . '.gif';
|
||||
break;
|
||||
}
|
||||
return md5_file($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $endTime
|
||||
* @param int|null $startTime
|
||||
* @return array
|
||||
* 剩余天,带分秒
|
||||
*/
|
||||
public static function timeout($endTime, int $startTime = null)
|
||||
{
|
||||
$endTime = $endTime - (!empty($startTime) ? $startTime : time());
|
||||
|
||||
$day = intval($endTime / (3600 * 24));
|
||||
|
||||
$hours = intval(($endTime - ($day * (3600 * 24))) / 3600);
|
||||
|
||||
$minute = intval(($endTime - ($day * (3600 * 24) + $hours * 3600)) / 60);
|
||||
|
||||
$scrod = intval(($endTime - ($day * (3600 * 24) + $hours * 3600 + $minute * 60)));
|
||||
|
||||
return [$day, $hours, $minute, $scrod];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return false|int
|
||||
*/
|
||||
public static function get_sy_time()
|
||||
{
|
||||
$time = strtotime('+1days', strtotime(date('Y-m-d')));
|
||||
|
||||
return $time - time();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
public static function encode(string $string)
|
||||
{
|
||||
return addslashes($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @return string|string[]|null
|
||||
* 清除标点符号
|
||||
*/
|
||||
public static function clear(string $string)
|
||||
{
|
||||
$char = '。、!?:;﹑•"…‘’“”〝〞∕¦‖— 〈〉﹞﹝「」‹›〖〗】【»«』『〕〔》《﹐¸﹕︰﹔!¡?¿﹖﹌﹏﹋'´ˊˋ―﹫︳︴¯_ ̄﹢﹦﹤‐˜﹟﹩﹠﹪﹡﹨﹍﹉﹎﹊ˇ︵︶︷︸︹︿﹀︺︽︾ˉ﹁﹂﹃﹄︻︼()';
|
||||
return preg_replace(array("/[[:punct:]]/i", '/[' . $char . ']/u', '/[ ]{2,}/'), '', $string);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $user
|
||||
* @param array $param
|
||||
* @param int $requestTime
|
||||
*
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function token($user, $param = [], $requestTime = NULL)
|
||||
{
|
||||
$str = '';
|
||||
if (!$requestTime) {
|
||||
$requestTime = microtime(true);
|
||||
}
|
||||
$_user = str_split(md5($user . md5($user)));
|
||||
ksort($_user);
|
||||
foreach ($_user as $key => $val) {
|
||||
$str .= md5(sha1($key . $val . 'www.xshucai.com'));
|
||||
}
|
||||
if (is_array($param)) {
|
||||
foreach ($param as $key => $val) {
|
||||
$str .= md5($str . sha1($key . md5($val)));
|
||||
}
|
||||
}
|
||||
$str .= sha1(base64_encode($requestTime));
|
||||
|
||||
$md5 = md5($str . $user);
|
||||
|
||||
return preg_replace('/(\w{10})(\w{3})(\w{4})(\w{9})(\w{6})/', '$1-$2-$3-$4-$5', $md5);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: admin
|
||||
* Date: 2019-03-20
|
||||
* Time: 01:03
|
||||
*/
|
||||
|
||||
namespace Snowflake\Core;
|
||||
|
||||
/**
|
||||
* Class Xml
|
||||
* @package Snowflake\Snowflake\Core
|
||||
*/
|
||||
class Xml
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @param bool $asArray
|
||||
* @return array|object
|
||||
*/
|
||||
public static function toArray($data, $asArray = true)
|
||||
{
|
||||
$data = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
|
||||
if ($asArray) {
|
||||
return json_decode(json_encode($data), TRUE);
|
||||
}
|
||||
|
||||
return json_decode(json_encode($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $str
|
||||
* @return bool
|
||||
*/
|
||||
public static function isXml(&$str)
|
||||
{
|
||||
$xml_parser = xml_parser_create();
|
||||
if (!xml_parse($xml_parser, $str, true)) {
|
||||
xml_parser_free($xml_parser);
|
||||
return false;
|
||||
} else {
|
||||
$str = self::toArray($str);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user