| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675 |
- <?php
- namespace common\helpers;
- use Yii;
- use yii\helpers\BaseStringHelper;
- use Ramsey\Uuid\Uuid;
- /**
- * Class StringHelper
- * @package common\helpers
- * @author qimall
- */
- class StringHelper extends BaseStringHelper
- {
- /**
- * 生成Uuid
- *
- * @param string $type 类型 默认时间 time/md5/random/sha1/uniqid 其中uniqid不需要特别开启php函数
- * @param string $name 加密名
- * @return string
- * @throws \Exception
- */
- public static function uuid($type = 'uniqid', $name = 'php.net')
- {
- switch ($type) {
- // php自带的唯一id
- case 'uniqid' :
- return md5(uniqid(md5(microtime(true) . self::random(8)), true));
- break;
- }
- return '';
- }
- /**
- * 日期转时间戳
- *
- * @param $value
- * @return false|int
- */
- public static function dateToInt($value)
- {
- if (empty($value)) {
- return $value;
- }
- if (!is_numeric($value)) {
- return strtotime($value);
- }
- return $value;
- }
- /**
- * 时间戳转日期
- *
- * @param $value
- * @return false|int
- */
- public static function intToDate($value, $format = 'Y-m-d H:i:s')
- {
- if (empty($value)) {
- return date($format);
- }
- if (is_numeric($value)) {
- return date($format, $value);
- }
- return $value;
- }
- /**
- * 获取缩略图地址
- *
- * @param string $url
- * @param int $width
- * @param int $height
- */
- public static function getThumbUrl($url, $width, $height)
- {
- $url = str_replace('attachment/images', 'attachment/thumb', $url);
- return self::createThumbUrl($url, $width, $height);
- }
- /**
- * 创建缩略图地址
- *
- * @param string $url
- * @param int $width
- * @param int $height
- */
- public static function createThumbUrl($url, $width, $height)
- {
- $url = explode('/', $url);
- $nameArr = explode('.', end($url));
- $url[count($url) - 1] = $nameArr[0] . "@{$width}x{$height}." . $nameArr[1];
- return implode('/', $url);
- }
- /**
- * 获取压缩图片地址
- *
- * @param $url
- * @param $quality
- * @return string
- */
- public static function getAliasUrl($url, $alias = 'compress')
- {
- $url = explode('/', $url);
- $nameArr = explode('.', end($url));
- $url[count($url) - 1] = $nameArr[0] . "@{$alias}." . $nameArr[1];
- return implode('/', $url);
- }
- /**
- * 根据Url获取本地绝对路径
- *
- * @param $url
- * @param string $type
- * @return string
- */
- public static function getLocalFilePath($url, $type = 'images')
- {
- if (RegularHelper::verify('url', $url)) {
- if (!RegularHelper::verify('url', Yii::getAlias("@attachurl"))) {
- $hostInfo = Yii::$app->request->hostInfo . Yii::getAlias("@attachurl");
- $url = str_replace($hostInfo, '', $url);
- } else {
- $url = str_replace(Yii::getAlias("@attachurl"), '', $url);
- }
- } else {
- $url = str_replace(Yii::getAlias("@attachurl"), '', $url);
- }
- return Yii::getAlias("@attachment") . $url;
- }
- /**
- * 分析枚举类型配置值
- *
- * 格式 a:名称1,b:名称2
- *
- * @param $string
- * @return array
- */
- public static function parseAttr($string)
- {
- $array = preg_split('/[,;\r\n]+/', trim($string, ",;\r\n"));
- if (strpos($string, ':')) {
- $value = [];
- foreach ($array as $val) {
- list($k, $v) = explode(':', $val);
- $value[$k] = $v;
- }
- } else {
- $value = $array;
- }
- return $value;
- }
- /**
- * 返回字符串在另一个字符串中第一次出现的位置
- *
- * @param $string
- * @param $find
- * @return bool
- * true | false
- */
- public static function strExists($string, $find)
- {
- return !(strpos($string, $find) === false);
- }
- /**
- * XML 字符串载入对象中
- *
- * @param string $string 必需。规定要使用的 XML 字符串
- * @param string $class_name 可选。规定新对象的 class
- * @param int $options 可选。规定附加的 Libxml 参数
- * @param string $ns
- * @param bool $is_prefix
- * @return bool|\SimpleXMLElement
- */
- public static function simplexmlLoadString(
- $string,
- $class_name = 'SimpleXMLElement',
- $options = 0,
- $ns = '',
- $is_prefix = false
- )
- {
- libxml_disable_entity_loader(true);
- if (preg_match('/(\<\!DOCTYPE|\<\!ENTITY)/i', $string)) {
- return false;
- }
- return simplexml_load_string($string, $class_name, $options, $ns, $is_prefix);
- }
- /**
- * 字符串提取汉字
- *
- * @param $string
- * @return mixed
- */
- public static function strToChineseCharacters($string)
- {
- preg_match_all("/[\x{4e00}-\x{9fa5}]+/u", $string, $chinese);
- return $chinese;
- }
- /**
- * 字符首字母转大小写
- *
- * @param $str
- * @return mixed
- */
- public static function strUcwords($str)
- {
- return str_replace(' ', '', ucwords(str_replace('-', ' ', $str)));
- }
- /**
- * 下划线转驼峰命名法风格
- *
- * @param $str
- * @param $separator // 被替换的字符
- * @param $ucfirst // 首字母是否大写
- * @return string
- */
- public static function camelize($str, $separator = '_', $ucfirst = false)
- {
- $str = str_replace($separator, " ", strtolower($str));
- $str = ltrim(str_replace(" ", "", ucwords($str)), $separator);
- return $ucfirst == true ? ucfirst($str) : lcfirst($str);
- }
- /**
- * 驼峰命名法转下划线风格
- *
- * @param $str
- * @return string
- */
- public static function toUnderScore($str)
- {
- $array = [];
- for ($i = 0; $i < strlen($str); $i++) {
- if ($str[$i] == strtolower($str[$i])) {
- $array[] = $str[$i];
- } else {
- if ($i > 0) {
- $array[] = '-';
- }
- $array[] = strtolower($str[$i]);
- }
- }
- return implode('', $array);
- }
- /**
- * 获取字符串后面的字符串
- *
- * @param string $fileName 文件名
- * @param string $type 字符类型
- * @param int $length 长度
- * @return bool|string
- */
- public static function clipping($fileName, $type = '.', $length = 0)
- {
- return substr(strtolower(strrchr($fileName, $type)), $length);
- }
- /**
- * 获取随机字符串
- *
- * @param $length
- * @param bool $numeric
- * @return string
- */
- public static function random($length, $numeric = false)
- {
- $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
- $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
- $hash = '';
- if (!$numeric) {
- $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
- $length--;
- }
- $max = strlen($seed) - 1;
- $seed = str_split($seed);
- for ($i = 0; $i < $length; $i++) {
- $hash .= $seed[mt_rand(0, $max)];
- }
- return $hash;
- }
- /**
- * 获取数字随机字符串
- *
- * @param bool $prefix 判断是否需求前缀
- * @param int $length 长度
- * @return string
- */
- public static function randomNum($prefix = false, $length = 8)
- {
- $str = $prefix ?? '';
- return $str . substr(implode(null, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, $length);
- }
- /**
- * 去除内容的注释
- *
- * @param $content
- * @return string|string[]|null
- */
- public static function removeAnnotation($content)
- {
- return preg_replace("/(\/\*(\s|.)*?\*\/)|(\/\/.(\s|.*))|(#(\s*)?(.*))/", '',
- str_replace(["\r\n", "\r"], "\n", $content));
- }
- /**
- * 生成随机code
- *
- * @param $merchant_id
- * @return false|string
- */
- public static function code($merchant_id)
- {
- $time_str = date('YmdHis');
- $rand_code = rand(0, 999999);
- return substr(md5($time_str . $rand_code . $merchant_id), 16, 32);
- }
- /**
- * 字符串匹配替换
- *
- * @param string $search 查找的字符串
- * @param string $replace 替换的字符串
- * @param string $subject 字符串
- * @param null $count
- * @return mixed
- */
- public static function replace($search, $replace, $subject, &$count = null)
- {
- return str_replace($search, $replace, $subject, $count);
- }
- /**
- * 验证是否Windows
- *
- * @return bool
- */
- public static function isWindowsOS()
- {
- return strncmp(PHP_OS, 'WIN', 3) === 0;
- }
- /**
- * 文字自动换行
- *
- * @param integer $fontsize 字体大小
- * @param integer $angle 角度
- * @param string $fontface 字体名称
- * @param string $string 字符串
- * @param integer $width 预设宽度
- * @param null $max_line
- * @return string
- */
- public static function autoWrap($font_size, $angle, $font_face, $string, $width, $max_line = null)
- {
- // 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
- $content = "";
- // 将字符串拆分成一个个单字 保存到数组 letter 中
- $letter = [];
- for ($i = 0; $i < mb_strlen($string, 'UTF-8'); $i++) {
- $letter[] = mb_substr($string, $i, 1, 'UTF-8');
- }
- $line_count = 0;
- foreach ($letter as $l) {
- $test_str = $content . " " . $l;
- $test_box = imagettfbbox($font_size, $angle, $font_face, $test_str);
- // 判断拼接后的字符串是否超过预设的宽度
- if (($test_box[2] > $width) && ($content !== "")) {
- $line_count++;
- if ($max_line && $line_count >= $max_line) {
- $content = mb_substr($content, 0, -1, 'UTF-8') . "...";
- break;
- }
- $content .= "\n";
- }
- $content .= $l;
- }
- return $content;
- }
- /**
- * 省略文字
- *
- * @param $text
- * @param int $num
- * @return string|string[]
- */
- public static function textOmit($string, $num = 26)
- {
- $letter = [];
- for ($i = 0; $i < mb_strlen($string, 'UTF-8'); $i++) {
- $letter[] = mb_substr($string, $i, 1, 'UTF-8');
- }
- $content = "";
- foreach ($letter as $key => $l) {
- if ($key + 1 == $num) {
- $content .= '...';
- break;
- }
- $content .= $l;
- }
- return $content;
- }
- /**
- * @param $string
- * @return string
- */
- public static function strToInt($string)
- {
- $versionArr = explode('.', $string);
- if (count($versionArr) > 3) {
- return false;
- }
- $version_id = 0;
- isset($versionArr[0]) && $version_id += BcHelper::mul((int)$versionArr[0], 100000000000, 12);
- isset($versionArr[1]) && $version_id += BcHelper::mul((int)$versionArr[1], 10000000, 8);
- isset($versionArr[2]) && $version_id += BcHelper::mul((int)$versionArr[2], 1000, 4);
- return $version_id;
- }
- /**
- * 将一个字符串部分字符用*替代隐藏
- *
- * @param string $string 待转换的字符串
- * @param int $bengin 起始位置,从0开始计数,当$type=4时,表示左侧保留长度
- * @param int $len 需要转换成*的字符个数,当$type=4时,表示右侧保留长度
- * @param int $type 转换类型:0,从左向右隐藏;1,从右向左隐藏;2,从指定字符位置分割前由右向左隐藏;3,从指定字符位置分割后由左向右隐藏;4,保留首末指定字符串
- * @param string $glue 分割符
- * @return bool|string
- */
- public static function hideStr($string, $bengin = 0, $len = 4, $type = 0, $glue = "@")
- {
- if (empty($string)) {
- return false;
- }
- $array = [];
- if ($type == 0 || $type == 1 || $type == 4) {
- $strlen = $length = mb_strlen($string);
- while ($strlen) {
- $array[] = mb_substr($string, 0, 1, "utf8");
- $string = mb_substr($string, 1, $strlen, "utf8");
- $strlen = mb_strlen($string);
- }
- }
- switch ($type) {
- case 0 :
- for ($i = $bengin; $i < ($bengin + $len); $i++) {
- isset($array[$i]) && $array[$i] = "*";
- }
- $string = implode("", $array);
- break;
- case 1 :
- $array = array_reverse($array);
- for ($i = $bengin; $i < ($bengin + $len); $i++) {
- isset($array[$i]) && $array[$i] = "*";
- }
- $string = implode("", array_reverse($array));
- break;
- case 2 :
- $array = explode($glue, $string);
- $array[0] = self::hideStr($array[0], $bengin, $len, 1);
- $string = implode($glue, $array);
- break;
- case 3 :
- $array = explode($glue, $string);
- $array[1] = self::hideStr($array[1], $bengin, $len, 0);
- $string = implode($glue, $array);
- break;
- case 4 :
- $left = $bengin;
- $right = $len;
- $tem = array();
- for ($i = 0; $i < ($length - $right); $i++) {
- if (isset($array[$i])) {
- $tem[] = $i >= $left ? "*" : $array[$i];
- }
- }
- $array = array_chunk(array_reverse($array), $right);
- $array = array_reverse($array[0]);
- for ($i = 0; $i < $right; $i++) {
- $tem[] = $array[$i];
- }
- $string = implode("", $tem);
- break;
- }
- return $string;
- }
- /**
- * 判断返回数据是否json
- * @param $string
- * @return bool
- */
- public static function isJson($string)
- {
- if (is_array($string) || is_object($string)) {
- return false;
- }
- json_decode($string);
- if (json_last_error() == JSON_ERROR_NONE) {
- //是否返回数组
- $data = json_decode($string, 1);
- return is_array($data);
- }
- return false;
- }
- public static function generateOrderNo($prefix = ''){
- // 优化唯一性保证:使用random_int()代替rand(),并结合微调以生成更唯一的ID
- $randInt = random_int(100000, 999999);
- // 日期时间戳处理
- $dateTimeStr = date('YmdHis');
- // 保证前缀不为空字符串且不超过一定长度(例如20个字符)
- $prefix = trim($prefix, '0');
- if (strlen($prefix) > 20) {
- $prefix = substr($prefix, 0, 20);
- }
- // 生成订单号,保证总长度为17(前缀+日期时间戳+随机数)
- $orderNo = $prefix . $dateTimeStr . str_pad($randInt, 6, '0', STR_PAD_LEFT);
- return $orderNo;
- }
- public static function createRandomString($length = 6)
- {
- if ($length <= 1) return '';
- $numberArr = range(1, 9);
- $lowercaseArr = range('a', 'z');
- $capitalArr = range('A', 'Z');
- $arr = array_merge($numberArr, $lowercaseArr, $capitalArr);
- shuffle($arr);
- $code = '';
- $codeArr = [];
- $arrLength = count($arr);
- for ($i = 0; $i < $length; $i++) {
- $rand = mt_rand(0, $arrLength - 1);
- $code .= $arr[$rand];
- $codeArr[] = $arr[$rand];
- }
- // 全部都是英文
- if (ctype_alpha($code)) {
- $replaceCount = rand(1, $length - 1);
- shuffle($codeArr);
- $codeLength = count($codeArr);
- for ($j = 1; $j <= $replaceCount; $j++) {
- $codeIndex = mt_rand(0, $codeLength - 1);
- shuffle($numberArr);
- $numberIndex = array_rand($numberArr);
- $codeArr[$codeIndex] = $numberArr[$numberIndex];
- }
- $code = implode($codeArr);
- }
- return $code;
- }
- public static function generateRandCode($length = 6)
- {
- $sourceString = '1234567890';
- $string = str_shuffle($sourceString); //打乱字符串
- $code = substr($string, 0, $length); //substr(string,start,length);返回字符串的一部分
- return $code;
- }
- /**
- * 通过版本号生产版本号数字
- */
- public static function getVersionNum($version = '')
- {
- if (empty($version)) {
- return false;
- }
- $versionArr = explode('.', $version);
- if (count($versionArr) > 3) {
- return false;
- }
- $versionNum = 0;
- $v1 = sprintf('%03s', (int) $versionArr[0]);
- $v2 = sprintf('%03s', (int) $versionArr[1]);
- $v3 = sprintf('%03s', (int) $versionArr[2]);
- return (int) "{$v1}{$v2}{$v3}";
- }
- /**
- * 生成随机姓名
- * @Author:lun
- * @Date:2024-07-02 11:14
- * @Copyright:copyright(c)2024 广东七件事集团
- * @return string
- */
- public static function randName()
- {
- $random = rand(1, 10);
- $first = ["赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "蒋", "沈", "韩", "杨", "朱", "秦", "许", "何", "吕", "胡", "张", "孔", "曹", "严", "魏", "陶", "姜", "戚", "谢", "邹", "林", "苏", "潘", "葛"];
- $last = ["梓", "秋", "胜", "智", "勇", "德", "欣", "意", "涵", "仪", "汐", "语", "颖", "铄", "景", "梦", "苒", "舒", "思", "沁", "如", "辰", "梵", "天", "钊", "恬", "玮", "泉", "静", "泽", "影", "瑞", "衡", "衿", "华", "新", "显", "昕", "柳", "然", "韬", "珊", "俊", "悦", "宜", "嘉", "琪", "惠", "山", "威", "赫", "瑶", "月", "斐", "致", "若"];
- if ($random <= 3) {// 两个字姓名跟三个字姓名占比大概按照3/7分
- $name = $first[array_rand($first)] . "*";
- return $name;
- } else {
- $name = $first[array_rand($first)] . "*" . $last[array_rand($last)];
- return $name;
- }
- }
- public static function getIdCardInfo($id_card)
- {
- $year = substr($id_card, 6, 4);
- $month = substr($id_card, 10, 2);
- $day = substr($id_card, 12, 2);
- $sex_code = substr($id_card, 16, 1);
- if ($sex_code % 2 == 0) {
- $sex = '女';
- } else {
- $sex = '男';
- }
- return ['birthday' => $year . '-' . $month . '-' . $day, 'sex' => $sex];
- }
- }
|