21個實用PHP代碼
- 威海Web網(wǎng)站開發(fā) 威海PHP
- 1202
1.重定向到指定頁面 header('Location: http://www.example.com/'); 2.設置HTTP響應頭 header('HTTP/1.1 404 Not Found'); 3.使用GET方式下載文件 4.獲取當前URL $current_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 5.絕對路徑轉相對路徑 $relative_path = substr($path, strlen(realpath('.')) + 1); 6.獲取 URL參數(shù) 7.獲取URL錨點 $url_fragment = parse_url($_SERVER['REQUEST_URI'], PHP_URL_FRAGMENT); 8.檢查字符串是否以指定字符串開頭 if (strpos($str, $prefix) === 0) 9.在字符串中搜索指定字符串 if (strpos($str, $needle) !== false) 10.在字符串中搜索指定字符串(區(qū)分大小寫) if (strpos($str, $needle) !== false) 11.取得字符串的長度 $str_length = strlen($str); 12.將字符串轉換為小寫 $str = strtolower($str); 13.將字符串轉換為大寫 $str = strtoupper($str); 14.將字符串中空白字符刪除 $str = preg_replace('/\s+/', '', $str); 15.生成一個隨機數(shù) $random_number = mt_rand(); 16.獲取當前日期 $current_date = date('Y-m-d'); 17.檢查是否通過HTTPS連接 if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) 18.讀取文件內(nèi)容 $file_contents = file_get_contents('path/to/file.txt'); 19.寫入文件 file_put_contents('path/to/file.txt', 'contents'); 20.以RFC 2822格式顯示當前時間 $rfc_2822_date = date('r'); 21.以Unix時間戳顯示當前時間 $unix_timestamp = time();