字符串转数组(php版)
思路:
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;
}
字符串转数组(php版)的更多相关文章
- shell入门笔记2:字符串、数组、echo与printf
说明: 本文是关于http://c.biancheng.net/cpp/shell/的相关笔记 shell字符串 字符串可以用单引号,也可以用双引号,也可以不用引号. 1 #!/bin/bash 2 ...
- sizeof、strlen、字符串、数组,整到一块,你还清楚吗?
写在前面 sizeof.strlen.字符串.数组,提到这些概念,相信学过C语言的人都能耳熟能详,也能谈得头头是道,但是,在实际运用中,当这些内容交织在一起时,大家却不一定能搞地清清楚楚,本文的目的正 ...
- Java中将一个字符串传入数组的几种方法
String Str="abnckdjgdag"; char a[]=new char[Str.length()]; -------------------方法1 用于取出字符串的 ...
- 03- Shell脚本学习--字符串和数组
字符串 字符串是shell编程中最常用最有用的数据类型(除了数字和字符串,也没啥其它类型好用了),字符串可以用单引号,也可以用双引号,也可以不用引号.单双引号的区别跟PHP类似: 单双引号的区别: 双 ...
- js中字符串和数组相互转化的方法
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #e4af0a } p. ...
- bash中不可以用字符串做数组下标
bash中可以用字符串做数组下标吗例如 test["abc"]=1------解决方案-------------------- 好像是误会,是awk里可以,bash shell里不 ...
- js数字、字符串、数组之间的转化
1.数组转字符串 var a, b; a = ,,,,); b = a.join("-"); 2.字符串转数组 var s = "abc,abcd,aaa"; ...
- C#.NET 字符串转数组,数组转字符串
string str = "1,2,3,4,5,6,7"; string[] strArray = str.Split(','); //字符串转数组 ...
- 固定分隔符字符串与数组互转及ArrayList与数组(Array)互转
1.字符串转数组 这个相信多数人都会常用,string.split方法,分隔符可以为多个.详细信息参见MSDN string[] actionCfgs = _para.Split(new char[] ...
随机推荐
- Java web 简单的增删改查程序(超详细)
就是简单的对数据进行增删改查.代码如下: 1.bean层:用来封装属性及其get set方法 toString方法,有参构造方法,无参构造方法等. public class Bean { privat ...
- Spring Boot教程(三十三)使用Redis数据库(1)
Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, Elasticsearch, So ...
- 关于数据库表设计之区域表system_district:省市县街道四级地址表
关于省市县的数据表的设计有两种方式: 一.将其设计成一张表 DROP TABLE IF EXISTS `system_district`; CREATE TABLE `system_district` ...
- Netty入门官方例子
参考链接:https://blog.csdn.net/wocjy/article/details/78661464 maven依赖: <!-- Netty开始 --> <!-- ht ...
- koa 基础(二十一)nodejs 操作mongodb数据库 --- 查询数据
1.app.js /** * nodejs 操作mongodb数据库 * 1.安装 操作mongodb * cnpm install mongodb --save * 2.引入 mongodb 下面的 ...
- storm备忘
[命令]storm rebalance topology-name [-w wait-time-secs] [-n new-num-workers] [-e component=parallelism ...
- MapInfo 文件解析
在MapInfo 中所指的表是单纯的数据表或是图形与数据的结合.一个典型的MapInfo表将主要由*.tab.*.dat.*.wks.*.dbf. *.xls.*.map.*.id.*.ind文件格式 ...
- ThinkPhp中验证码不显示和配置项的问题解决方法
1.验证码不显示在调用验证码之前加上 ob_clean();像这样: public function verify(){ ob_clean(); $verify = new \Think\Verify ...
- LC 789. Escape The Ghosts
You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is(tar ...
- javascript之Math对象
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...