输出PHP可直接使用数组 正则替换 牛逼的正则表达式

20 篇文章 0 订阅
5 篇文章 0 订阅

需求:调试过程中,需要用到接口数据,又无法频繁请求接口的时候,这时怎么办呢?

思路1:按照打印数据,一步一步手打出所需数组,然后进行调试。

思路2:写个方法来处理接口数据,将之转化成php里面可以直接使用的数组,然后从浏览器复制粘贴到编辑器里面,接着进行调试。

好了,都是程序员了,肯定怎么偷懒怎么来了。我选择第二种:


/**
 * 输出PHP可直接使用数组 => 正则替换
 * @param array $var  数组
 * @param int   $mode 二进制数  1-双引号变为单引号 2-去掉数字键 3-去掉字符串键
 */
function dump_array(array $var, int $mode = 1): void
{
    ob_start();
    var_dump($var);
    $output = ob_get_clean();

    // 处理键值 [1] => 1, ["aaa"] => "aaa"
    $output = preg_replace('/\[(.*)]=>\s+/', '$1 => ', $output);

    // 处理null类型 NULL => NULL,
    $output = preg_replace('/=> NULL/', '=> NULL,', $output);

    // 处理int类型 int(1) => 1,
    $output = preg_replace('/=> int\((\d+)\)/', '=> $1,', $output);

    // 处理float类型 float(0.1) => 0.1,
    $output = preg_replace('/=> float\((.*)\)/', '=> $1,', $output);

    // 处理string类型 string(1) "2" => "2",
    $output = preg_replace('/=> string\(\d+\) (.*)/', '=> $1,', $output);

    // 处理array类型 array(3) { => [
    $output = preg_replace('/array\(\d+\) {$/m', '[', $output);

    // 处理数组“}” } => ],
    $output = preg_replace('/(\s*)}(\s+)/', '$1],$2', $output);

    // 处理末尾“],” ], => ];
    $output = preg_replace('/(\v]),/', '$1;', $output);

    // 双引号变为单引号
    if ($mode & 1) {
        // 处理键值 双引号变为单引号 " => '
        $output = preg_replace('/"(.*)" => /', '\'$1\' => ', $output);

        // 处理字符串 双引号变为单引号 " => '
        $output = preg_replace('/=> "(.*\s*)"/', '=> \'$1\'', $output);
    }

    // 去掉数字键
    if ($mode & 2) {
        $output = preg_replace('/\d+ => /', '', $output);
    }

    // 去掉字符串键
    if ($mode & 4) {
        $output = preg_replace('/\'\w+\' => /', '', $output);
    }

    echo '<pre>' . $output . '</pre>';
}


// 以下为测试数据
$json = <<<'EOT'
{
	"itemTitle": "【百亿补贴】福临门营养家活粒鲜胚玉米胚芽油 700ml",
	"minPoints": null,
	"descOption": "<p><imgggg align=\"absmiddle\" src=\"https://img.alicdn.com/imgextra/i4/1865856284/O1CN01nlbHsd1wI7WF8mtQp_!!1865856284.jpg\" style=\"max-width: 750.0px;\"><imgggg align=\"absmiddle\" src=\"https://img.alicdn.com/imgextra/i1/1865856284/O1CN01fAmxJX1wI7WPfJiCr_!!1865856284.jpg\" style=\"max-width: 750.0px;\"><imgggg align=\"absmiddle\" src=\"https://img.alicdn.com/imgextra/i4/1865856284/O1CN01yyZgYT1wI7WSL3eR9_!!1865856284.jpg\" style=\"max-width: 750.0px;\"><imgggg align=\"absmiddle\" src=\"https://img.alicdn.com/imgextra/i2/1865856284/O1CN013KeTJJ1wI7WOaaDfS_!!1865856284.jpg\" style=\"max-width: 750.0px;\"><imgggg align=\"absmiddle\" src=\"https://img.alicdn.com/imgextra/i1/1865856284/O1CN01mJHwxm1wI7WSL2qYm_!!1865856284.jpg\" style=\"max-width: 750.0px;\"><imgggg align=\"absmiddle\" src=\"https://img.alicdn.com/imgextra/i3/1865856284/O1CN01EEWlBy1wI7WPfIySg_!!1865856284.jpg\" style=\"max-width: 750.0px;\"></p>",
	"itemImages": [
		"//img.alicdn.com/imgextra/i3/1865856284/O1CN01yGTPoj1wI7WOaXjVD_!!1865856284.jpg",
		"//img.alicdn.com/imgextra/i2/1865856284/O1CN01IH6kBM1wI7WTcGo55_!!1865856284.jpg",
		"//img.alicdn.com/imgextra/i1/1865856284/O1CN01ijCk4z1wI7WQkmWt1_!!1865856284.jpg",
		"//img.alicdn.com/imgextra/i2/1865856284/O1CN01E7y0Qb1wI7WLY2tTF_!!1865856284.jpg"
	],
	"extJson": {
		"delivery_version": "1",
		"extraPeriod": "540",
		"food_pro_date": "2020-06-01~2020-06-30"
	},
	"isSellerPayPostfee": 0,
	"lmItemCategory": "1",
	"sellerPayPostfee": 0,
	"reservePrice": "1990",
	"quantity": "39",
	"customizedAttributeMap": null,
	"iforestPropsJson": "[{\"value\":\"福临门\",\"key\":\"品牌\"},{\"value\":\"营养家活粒鲜胚玉米胚芽油\",\"key\":\"系列\"},{\"value\":\"700ml\",\"key\":\"规格\"}]",
	"propertiesJson": "{\"产地\":[\"中国大陆\"],\"品牌\":[\"福临门\"],\"省份\":[\"安徽省\"],\"系列\":[\"营养家活粒鲜胚玉米胚芽油\"],\"套餐周期\":[\"1周\"],\"配送频次\":[\"1周2次\"],\"规格\":[\"700ml\"],\"净含量\":[\"700ml\"],\"包装方式\":[\"包装\"],\"城市\":[\"蚌埠市\"],\"套餐份量\":[\"1人份\"]}",
	"iforestProps": [
		{
			"value": "福临门",
			"key": "品牌"
		},
		{
			"value": "营养家活粒鲜胚玉米胚芽油",
			"key": "系列"
		},
		{
			"value": "700ml",
			"key": "规格"
		}
	],
	"tbShopName": "好食期特惠食品",
	"sellerId": "1865856284",
	"lmItemId": "10000021-634103129649",
	"canSell": 1,
	"itemId": "634103129649",
	"centerInventory": "1",
	"totalSoldQuantity": "820",
	"mainPicUrl": "//img.alicdn.com/imgextra/i3/1865856284/O1CN01yGTPoj1wI7WOaXjVD_!!1865856284.jpg",
	"minPrice": "1990",
	"isCanSell": 1,
	"categoryId": "50050380",
	"descPath": "//img.alicdn.com/imgextra/i1/630/100/634103129649/TB14AiS41L2gK0jSZPh8quhvXla",
	"properties": {
		"产地": [
			"中国大陆"
		],
		"品牌": [
			"福临门"
		],
		"省份": [
			"安徽省"
		],
		"系列": [
			"营养家活粒鲜胚玉米胚芽油"
		],
		"套餐周期": [
			"1周"
		],
		"配送频次": [
			"1周2次"
		],
		"规格": [
			"700ml"
		],
		"净含量": [
			"700ml"
		],
		"包装方式": [
			"包装"
		],
		"城市": [
			"蚌埠市"
		],
		"套餐份量": [
			"1人份"
		]
	},
	"skus": [
		{
			"priceCent": "1990",
			"lmItemId": "10000021-634103129649",
			"pointPrice": "1990",
			"itemId": "634103129649",
			"canSell": 1,
			"skuTitle": null,
			"extJson": null,
			"skuPropertities": null,
			"skuId": "-1",
			"skuPicUrl": "//img.alicdn.com/imgextra/i3/1865856284/O1CN01yGTPoj1wI7WOaXjVD_!!1865856284.jpg",
			"points": "0",
			"reservePrice": "1990",
			"pointsAmount": "0",
			"quantity": "39",
			"customizedAttributeMap": []
		}
	]
}
EOT;

$arr = json_decode($json, true);

dump_array($arr);

以下为运行结果;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值