這 10 個片段覆蓋搜索、營運、安全、前端、工具、運維六大高頻場景,複製即用,解決 80% 臨時需求。
function highlight(string $text, array $keywords): string {
if (empty($keywords) || $text === '') return $text;
$pattern = '/\b(' . implode('|', array_map('preg_quote', $keywords)) . ')\b/i';
return preg_replace($pattern, '<mark>$1</mark>', $text);
}
function get_feedburner_avg(string $feedId, int $days = 7): int {
$today = date('Y-m-d');
$ago = date('Y-m-d', strtotime("-$days days"));
$url = "https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=$feedId&dates=$ago,$today";
$xml = simplexml_load_string(file_get_contents($url));
$total = 0;
foreach ($xml->feed->entry as $entry) {
$total += (int)$entry['circulation'];
}
return round($total / $days);
}
function generatePassword(int $length = 12, int $strength = 4): string {
$vowels = 'aeuy';
$cons = 'bdghjmnpqrstvz';
if ($strength >= 1) $cons .= 'BDGHJLMNPQRSTVWXZ';
if ($strength >= 2) $vowels .= 'AEUY';
if ($strength >= 4) $cons .= '23456789';
if ($strength >= 8) $vowels .= '@#$%';
$pool = $vowels . $cons;
$pwd = '';
for ($i = 0; $i < $length; $i++) {
$pwd .= $pool[random_int(0, strlen($pool) - 1)];
}
return $pwd;
}
header('Content-Type: text/css; charset=utf-8');
function compress_css(string $css): string {
$css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css); // 移除註釋
$css = preg_replace('/\s+/', ' ', $css); // 壓縮空白
return trim($css);
}
$files = ['css/reset.css', 'css/layout.css', 'css/theme.css', 'css/print.css'];
$output = '';
foreach ($files as $file) {
if (is_readable($file)) $output .= file_get_contents($file);
}
echo compress_css($output);
function getTinyUrl(string $url): string {
return file_get_contents("https://tinyurl.com/api-create.php?url=" . urlencode($url));
}
function ageFromBirthdate(string $birthdate): int {
$birth = strtotime($birthdate);
if ($birth === false) return 0;
$today = time();
$years = date('Y', $today) - date('Y', $birth);
$mdToday = date('md', $today);
$mdBirth = date('md', $birth);
if ($mdToday < $mdBirth) $years--;
return $years;
}
$start = hrtime(true); // PHP 7.3+ 高精度
// 你的業務代碼
$end = hrtime(true);
echo 'Execution time: ' . (($end - $start) / 1e9) . ' seconds';
function maintenanceMode(bool $enable = true): void {
$maintenanceFile = __DIR__ . '/maintenance.php';
$isMaintenance = basename($_SERVER['SCRIPT_FILENAME']) === 'maintenance.php';
if ($enable && !$isMaintenance) {
header('Location: /maintenance.php', true, 503);
exit;
}
if (!$enable && $isMaintenance) {
header('Location: /', true, 302);
exit;
}
}
// 入口文件最早調用
maintenanceMode(true); // 開啟維護
// maintenanceMode(false); // 關閉維護
<link href="/css/app.css?v=<?= time() ?>" rel="stylesheet">
<script src="/js/app.js?v=<?= time() ?>"></script>
// 生產環境建議用文件哈希/構建版本號替代 time()
// <link href="/css/app.css?v=<?= filemtime('public/css/app.css') ?>">
function toOrdinal(int $num): string {
$lastTwo = $num % 100;
if ($lastTwo >= 11 && $lastTwo <= 13) return $num . 'th';
switch ($num % 10) {
case 1: return $num . 'st';
case 2: return $num . 'nd';
case 3: return $num . 'rd';
default: return $num . 'th';
}
}
app/Helpers/Functions.php,composer.json autoload.files 載入。mark { background: #ff0; })。error_page 503 /maintenance.html 靜態頁,完全繞過 PHP。這 10 個片段,是每個 PHP 開發者工具箱裡的「創可貼」——小傷小痛,貼上即愈,專注核心業務。