收集、管理和分享有用的代码片段,提高开发效率
根据生日计算年龄
<?php
function calculateAge($birthday) {
$birth = new DateTime($birthday);
$now = new DateTime;
$age = $now->diff($birth);
return $age->y;
}
检查密码强度
<?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
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
function generatePassword($length = 12) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$&';
$pass = '';
for ($i = 0; $i < $length; $i++) {
$pass .= $chars[rand(0, strlen($chars) - 1)];
}
return $pass;
}
转换日期到不同时区
<?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');
}
高效管理您的代码片段,提高开发效率
通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能
支持多种编程语言的语法高亮,使代码更加清晰易读
随时随地访问您的代码片段库,支持桌面和移动设备
浏览最受欢迎的代码分类