代码改变世界

实用的PHP代码片段收集

2013-08-21 17:18  楼台别  阅读(285)  评论(0)    收藏  举报

实用的PHP代码片段收集:
1.PHP显示 Youtube 或 Vimeo 视频缩略图 function video_image($url){
   $image_url = parse_url($url);
     if($image_url['host'] == 'www.youtube.com' ||
        $image_url['host'] == 'youtube.com'){
         $array = explode("&", $image_url['query']);
        ], 2)."/0.jpg";
     }else if($image_url['host'] == 'www.youtu.be' ||
              $image_url['host'] == 'youtu.be'){
         $array = explode("/", $image_url['path']);
              }else if($image_url['host'] == 'www.vimeo.com' ||
         $image_url['host'] == 'vimeo.com'){
         $hash = unserialize(file_get_contents("
         substr($image_url['path'], 1).".php"));
         return $hash[0]["thumbnail_medium"];
     }
}
 
<img src="<?php echo video_image('youtube URL'); ?>" />2.PHP生成随机密码
//方法1诚信在线http://t.qq.com/cx6899com
echo substr(md5(uniqid()), 0, 8);
 
//方法2
function rand_password($length){
  $chars =  'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  $chars .= '0123456789' ;
  
  $str = '';
  $max = strlen($chars) - 1;
 
  for ($i=0; $i < $length; $i++)
    $str .= $chars[rand(0, $max)];
 
  return $str;
}
 
echo rand_password(16);3.PHP文件解压
<?php
$zip = zip_open("moooredale.zip");
  if ($zip) {
   while ($zip_entry = zip_read($zip)) {
   $fp = fopen(zip_entry_name($zip_entry), "w");
   if (zip_entry_open($zip, $zip_entry, "r")) {
   $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
   fwrite($fp,"$buf");
   zip_entry_close($zip_entry);
   fclose($fp);
 }
}
zip_close($zip);
}
?>4.PHP转换秒到日期、时或者分 function seconds2days($mysec) {
    $mysec = (int)$mysec;
    if ( $mysec === 0 ) {
        return '0 second';
    }
 
    $mins  = 0;
    $hours = 0;
    $days  = 0;
 
 
    if ( $mysec >= 60 ) {
        $mins = (int)($mysec / 60);
        $mysec = $mysec % 60;
    }
    if ( $mins >= 60 ) {
        $hours = (int)($mins / 60);
        $mins = $mins % 60;
    }
    if ( $hours >= 24 ) {
        $days = (int)($hours / 24);
        $hours = $hours % 60;
    }
 
    $output = '';
 
    if ($days){
        $output .= $days." days ";
    }
    if ($hours) {
        $output .= $hours." hours ";
    }
    if ( $mins ) {
        $output .= $mins." minutes ";
    }
    if ( $mysec ) {
        $output .= $mysec." seconds ";
    }
    $output = rtrim($output);
    return $output;
}