代码片段仓库

收集、管理和分享有用的代码片段,提高开发效率

代码片段

计算年龄 PHP

根据生日计算年龄

<?php
function calculateAge($birthday) {
    $birth = new DateTime($birthday);
    $now = new DateTime;
    $age = $now->diff($birth);
    return $age->y;
}
密码强度 PHP

检查密码强度

<?php
function checkPasswordStrength($password) {
    $score = 0;
    if (strlen($password) >= 8) $score+= 25;
    if (preg_match('/[A-Z]/', $password)) $score += 25;
    if (preg_match('/[0-9]/', $password)) $score += 25;
    if (preg_match('/[^a-zA-Z0-9]/'/', $password)) $score += 25;
    return $score;
}
数组分页 PHP

对数组进行分页处理

<?php
function arrayPaginate($array, $page = 1, $limit = 10) {
    $total = count($array);
    $totalPages = ceil($total / $limit);
    $offset = ($page - 1) * $limit;
    $result = array_slice($array, $offset, $limit);
    return [
        'data' => $result,
        'total' => $total,
        'pages' => $totalPages
    ];
}
随机密码 PHP

生成安全随机密码

<?php
function generatePassword($length = 12) {
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$&';
    $pass = '';
    for ($i = 0; $i < $length; $i++) {
        $pass .= $chars[rand(0, strlen($chars) - 1)];
    }
    return $pass;
}
时区转换 PHP

转换日期到不同时区

<?php
function convertTimezone($date, $fromZone, $toZone) {
    $date = new DateTime($date, new DateTimeZone($fromZone));
    $date->setTimeZone(new DateTimeZone($toZone));
    return $date->format('Y-m-d H:i:s');
}

为什么选择CodeSnippets?

高效管理您的代码片段,提高开发效率

智能搜索

通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能

语法高亮

支持多种编程语言的语法高亮,使代码更加清晰易读

多设备同步

随时随地访问您的代码片段库,支持桌面和移动设备

热门分类

浏览最受欢迎的代码分类