最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php开发中实用的PHP代码片段
时间:2022-06-24 17:34:54 编辑:袖梨 来源:一聚教程网
1.关键词高亮
代码如下 | 复制代码 |
function highlight($sString, $aWords) { $sWords = implode ('|', $aWords); |
2.获取你的Feedburner的用户
代码如下 | 复制代码 |
function get_average_readers($feed_id,$interval = 7){ $nb = 0; return round($nb/$interval); |
3.自动生成密码
代码如下 | 复制代码 |
function generatePassword($length=9, $strength=0) { $password = ''; |
4.压缩多个CSS文件
代码如下 | 复制代码 |
header('Content-type: text/css'); /* your css files */ ob_end_flush(); |
5.获取短网址
代码如下 | 复制代码 |
function getTinyUrl($url) { return file_get_contents("http://tinyurl.com/api-create.php?url=".$url); } |
6.根据生日计算年龄
代码如下 | 复制代码 |
function age($date){ $date = date('Y-m-d', $time); return $year_diff; |
7.计算执行时间
代码如下 | 复制代码 |
//Create a variable for start time // Place your PHP/HTML/JavaScript/CSS/Etc. Here //Create a variable for end time echo 'Script took '.$time.' seconds to execute';8.PHP的维护模式 if(basename($_SERVER['SCRIPT_FILENAME']) != 'maintenance.php'){ |
9.阻止CSS样式被缓存
代码如下 | 复制代码 |
function make_ranked($rank) { if( $last == 1 && $seclast == 1) $ext = 'th'; return $rank.$ext; |
通过IP判断来源
这是一个非常实用的代码片段,可以帮助你通过IP来判断访客来源。下面的方法通过接收一个参数,然后返回IP所在地点。如果没有找到,则返回UNKNOWN。
代码如下 | 复制代码 |
function detect_city($ip) { $default = 'UNKNOWN'; if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost') $ip = '8.8.8.8'; $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)'; $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip); $ch = curl_init(); $curl_opt = array( CURLOPT_FOLLOWLOCATION => 1, CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => 1, CURLOPT_USERAGENT => $curlopt_useragent, CURLOPT_URL => $url, CURLOPT_TIMEOUT => 1, CURLOPT_REFERER => 'http://' . $_SERVER['HTTP_HOST'], ); curl_setopt_array($ch, $curl_opt); $content = curl_exec($ch); if (!is_null($curl_info)) { $curl_info = curl_getinfo($ch); } curl_close($ch); if ( preg_match('{ $city = $regs[1]; } if ( preg_match('{ $state = $regs[1]; } if( $city!='' && $state!='' ){ $location = $city . ', ' . $state; return $location; }else{ return $default; } } |
判断一张图片的主色调
下面这个代码非常实用,能帮助你判断一张图片中的主色调,你可以分析任何图片。
代码如下 | 复制代码 |
$i = imagecreatefromjpeg("image.jpg"); for ($x=0;$x $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> & 0xFF; $b = $rgb & 0xFF; $rTotal += $r; $gTotal += $g; $bTotal += $b; $total++; } } $rAverage = round($rTotal/$total); $gAverage = round($gTotal/$total); $bAverage = round($bTotal/$total); |
不显示PHP错误而发送电子邮件取代之
如果你不想在页面中显示PHP错误,也可以通过email来获取错误信息。下面的代码可以帮助你实现。
代码如下 | 复制代码 |
// Our custom error handler function nettuts_error_handler($number, $message, $file, $line, $vars){ $email = " An error ($number) occurred on line $message ";$email .= " " . print_r($vars, 1) . ""; $headers = 'Content-type: text/html; charset=iso-8859-1' . "rn"; // Email the error to someone... error_log($email, 1, '[email protected]', $headers); // Make sure that you decide how to respond to errors (on the user's side) // Either echo an error message, or kill the entire project. Up to you... // The code below ensures that we only "die" if the error was more than // just a NOTICE. if ( ($number !== E_NOTICE) && ($number < 2048) ) { die("There was an error. Please try again later."); } } // We should use our custom function to handle errors. set_error_handler('nettuts_error_handler'); // Trigger an error... (var doesn't exist) echo $somevarthatdoesnotexist; |
相关文章
- 《无限暖暖》织衣蛛获得方法 12-26
- 《无限暖暖》美食家蜜蜂获得方法 12-26
- 《无限暖暖》泳镜蝽获得方法 12-26
- 《无限暖暖》芭蕾豆娘获得方法 12-26
- 《无限暖暖》刺绣蛾获得方法 12-26
- 《无限暖暖》尖帽仙获得方法 12-26