思路:

1.判断当前传来的值是否为数组

2.若不是现将传来的值转换为字符串类型

3.判断当前值是否为空

4.若不为空,采用正则进行匹配,如下图

preg_match('/^{.*?}$/', $string) || preg_match('/^\[.*?]$/', $string) || preg_match('/^a:.*?(})$/', $string)

5.若正则无法匹配,则采用查找首次字符串出现的位置进行拆分分割

strpos($string, $delimiter) >= 1

具体代码示例如下

    /**
* 字符串、数组转换为格式化的数组
* @param string|array $data 原始字符串,可以为数组、 json字符串、 序列化字符串
* @param string $delimiter 字符串分隔符,默认为英文逗号
* @return array
*/
function cm_unserialize($data, string $delimiter = ','): array
{ // 数组原样返回
if (is_array($data)) {
return $data;
}
// 字符串处理
$string = (string)$data;
if (empty($string)) {
$result = [];
} else if (preg_match('/^{.*?}$/', $string) || preg_match('/^\[.*?]$/', $string)) {
$result = json_decode($string, true);
} else if (preg_match('/^a:.*?(})$/', $string)) {
$result = unserialize($string, null);
} else if (strpos($string, $delimiter) >= 1) {
$result = explode($delimiter, $string);
} else {
$result = [];
} if (!is_array($result) || count($result) < 1) {
return [];
}
return $result;
}

最新文章

  1. php 错误
  2. javascript中关于日期和时间的基础知识
  3. Centos ftp服务器安装配置
  4. web工程spring+ibatis单元测试
  5. PageBean分页组件
  6. paper 27 :图像/视觉显著性检测技术发展情况梳理(Saliency Detection、Visual Attention)
  7. selenium验证码处理
  8. C# 之 后台加载图片Image
  9. asp.net中WebForm.aspx与类文件分离使用
  10. ZeroBraneStudio之支持远程调试
  11. 【性能测试】【Jmeter】学习(2)——录制一段脚本
  12. Unicode的解救方案 - Windows程序设计(SDK)002
  13. lucas定理解决大组合数取模
  14. typeof、constructor和instance
  15. 10 Easy Steps to a Complete Understanding of SQL
  16. Jest 学习笔记(一)之matchers
  17. Spring使用Quartz定时调度Job无法Autowired注入Service的解决方案
  18. 三款免费实用的文件夹同步/备份软件推荐 (SyncToy/FreeFileSync/Compare Advance)
  19. hdu1003 Max Sum【最大连续子序列之和】
  20. Python3学习之路~5.8 shelve模块

热门文章

  1. AtCoder AGC004F Namori (图论)
  2. 输出变量的界值(int、float、long.....)
  3. onReachBottom 注意事项
  4. TCP连接建立 之 同时打开
  5. Nginx-rtmp点播之complex handshake
  6. STL -- heap结构及算法
  7. VMware安装CentOS后无法无法识别网卡的解决方法
  8. 3709: [PA2014]Bohater
  9. ubuntu关于ssh协议登录问题
  10. Cookie实战案例代码