php常用函数

/**
 * 格式化金额
 */
function format_amount($float)
{
    if ($float == intval($float)) {
        return intval($float);
    } elseif ($float == sprintf('%.1f', $float)) {
        return sprintf('%.1f', $float);
    }
    return $float;
}


/**
 * 判断是否是json
 */
function is_json($str)
{
    if (is_string($str)) {
        $json = json_decode($str, true);
        if (is_array($json) || is_object($json)) {
            return true;
        }
    }
    return false;
}
//post
function curlPost($url,$data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

//异步post
function asyncPost($url, $data) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1); // 设置超时为1秒

    // 启动异步处理
    curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);

    // 执行请求,不等待响应
    curl_exec($ch);

    // 关闭cURL资源,并且释放系统资源
    curl_close($ch);
}

//隐藏手机号中间四位
function hidePhoneNumber($phoneNumber) {
    // 确保输入的是有效的手机号(这里以11位手机号为例)
    if(strlen($phoneNumber) != 11) {
        return $phoneNumber;
    }

    // 保留前三位和后四位,隐藏其他数字
    $hiddenNumber = substr($phoneNumber, 0, 3) . str_repeat('*', 4) . substr($phoneNumber, -4);

    return $hiddenNumber;
}

//格式化文件大小
function formatFileSize($size): string
{
    if ($size <= 0) return '0 B';
    $units = array('B', 'KB', 'MB', 'GB', 'TB');
    $factor = floor((strlen($size) - 1) / 3);
    return round($size / pow(1024, $factor), 2) . ' ' . $units[$factor];
}
  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值