<?php
* @Descripttion: 自定义助手函数
* @Author: YouHuJun
* @Date: 2020-02-20 11:25:39
* @LastEditors: youhujun youhu8888@163.com & xueer
* @LastEditTime: 2026-03-15 14:51:34
*/
if (!function_exists('p')) {
* 打印函数(格式化输出,便于调试)
*
* @param mixed $param 要打印的参数,可以是任意类型
* @return void
*/
function p($param): void
{
echo "<pre>";
print_r($param);
echo "</pre>";
}
}
if (!function_exists('f')) {
* 过滤字符串中的HTML标签/转义HTML实体
*
* @param mixed $param 输入的字符串或数组
* @param int $type 过滤类型:0=转义实体(默认),1=去除标签
* @return mixed 过滤后的字符串/数组
*/
function f($param, $type = 0)
{
if (is_array($param)) {
foreach ($param as $key => $value) {
$param[$key] = f($value, $type);
}
return $param;
}
if ($type === 1) {
return strip_tags((string)$param);
}
return htmlspecialchars((string)$param, ENT_QUOTES, 'UTF-8');
}
}
if (!function_exists('code')) {
* 合并接口返回码和附加数据
*
* @param array|null $code 配置的返回码数组
* @param array|null $add 附加数据数组
* @return array 合并后的结果
*/
function code($code = [], $add = [])
{
$code = $code ?? [];
$add = $add ?? [];
return array_merge($code, $add);
}
}
if (!function_exists('is_serialized')) {
* 检测字符串是否为PHP序列化格式
*
* @param mixed $data 待检测数据
* @return bool
*/
function is_serialized($data)
{
if (!is_string($data)) {
return false;
}
$data = trim($data);
if ($data === 'N;') {
return true;
}
if (!preg_match('/^([adObis]):/', $data, $matches)) {
return false;
}
$type = $matches[1];
switch ($type) {
case 'a':
case 'O':
case 's':
return preg_match("/^{$type}:[0-9]+:.*[;}]\$/s", $data);
case 'b':
case 'i':
case 'd':
return preg_match("/^{$type}:[0-9.E-]+;\$/", $data);
default:
return false;
}
}
}
if (!function_exists('array_level')) {
* 计算数组维度
*
* @param array $arr 待计算数组
* @return int 数组最大维度
*/
function array_level(array $arr): int
{
$levels = [0];
total($arr, $levels);
return max($levels);
}
}
if (!function_exists('total')) {
* 递归计算数组维度(辅助函数)
*
* @param mixed $arr 待检测数据
* @param array &$levels 存储维度的引用数组
* @param int $level 当前层级
* @return void
*/
function total($arr, &$levels, $level = 0): void
{
if (is_array($arr)) {
$level++;
$levels[] = $level;
foreach ($arr as $v) {
total($v, $levels, $level);
}
}
}
}
if (!function_exists('to_array')) {
* 将数组每个元素转为单元素数组
*
* @param mixed $array 输入数据(非数组直接返回)
* @return array 处理后的数组
*/
function to_array($array): array
{
if (!is_array($array)) {
return [];
}
foreach ($array as $k => &$v) {
$v = [$v];
}
unset($v);
return $array;
}
}
if (!function_exists('check_id')) {
* 检查ID是否为纯数字
*
* @param mixed $id 待检查ID
* @return int 有效ID返回自身,无效返回0
*/
function check_id($id): int
{
return preg_match('/^\d+$/', (string)$id) ? (int)$id : 0;
}
}