Rumah >pembangunan bahagian belakang >tutorial php >一些很有用的 PHP 代码片段

一些很有用的 PHP 代码片段

WBOY
WBOYasal
2016-07-25 08:50:25980semak imbas
  1. $host="localhost";
  2. $uname="database username";
  3. $pass="database password";
  4. $database = "database name";
  5. $connection=mysql_connect($host,$uname,$pass)
  6. or die("Database Connection Failed");
  7. $result=mysql_select_db($database)
  8. or die("database cannot be selected");
  9. ?>
复制代码
  1. function words_limit( $str, $num, $append_str='' ){
  2. $words = preg_split( '/[\s]+/', $str, -1, PREG_SPLIT_OFFSET_CAPTURE );
  3. if( isset($words[$num][1]) ){
  4. $str = substr( $str, 0, $words[$num][1] ).$append_str;
  5. }
  6. unset( $words, $num );
  7. return trim( $str );>
  8. }
  9. echo words_limit($yourString, 50,'...');
  10. or
  11. echo words_limit($yourString, 50);
复制代码
  1. function video_image($url){
  2. $image_url = parse_url($url);
  3. if($image_url['host'] == 'www.youtube.com' ||
  4. $image_url['host'] == 'youtube.com'){
  5. $array = explode("&", $image_url['query']);
  6. return "http://img.youtube.com/vi/".substr($array[0], 2)."/0.jpg";
  7. }else if($image_url['host'] == 'www.youtu.be' ||
  8. $image_url['host'] == 'youtu.be'){
  9. $array = explode("/", $image_url['path']);
  10. return "http://img.youtube.com/vi/".$array[1]."/0.jpg";
  11. }else if($image_url['host'] == 'www.vimeo.com' ||
  12. $image_url['host'] == 'vimeo.com'){
  13. $hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/".
  14. substr($image_url['path'], 1).".php"));
  15. return $hash[0]["thumbnail_medium"];
  16. }
  17. }
  18. 一些很有用的 PHP 代码片段
复制代码
  1. function age_from_dob($dob){
  2. $dob = strtotime($dob);
  3. $y = date('Y', $dob);
  4. if (($m = (date('m') - date('m', $dob))) $y++;
  5. } elseif ($m == 0 && date('d') - date('d', $dob) $y++;
  6. }
  7. return date('Y') - $y;
  8. }
  9. echo age_from_dob('2005/04/19'); date in yyyy/mm/dd format.
复制代码
  1. //设置 Cookie
  2. setcookie("name", 'value', time()+3600*60*30);
  3. //显示 Cookie
  4. if ($_COOKIE["name"]!=""){
  5. $_SESSION['name'] = $_COOKIE["name"];
  6. }
复制代码
  1. //方法1
  2. echo substr(md5(uniqid()), 0, 8);
  3. //方法2
  4. function rand_password($length){
  5. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  6. $chars .= '0123456789' ;
  7. $chars .= '!@#%^&*()_,./?;:[]{}\|=+';
  8. $str = '';
  9. $max = strlen($chars) - 1;
  10. for ($i=0; $i $str .= $chars[rand(0, $max)];
  11. return $str;
  12. }
  13. echo rand_password(16);
复制代码
  1. date_default_timezone_set("Asia/Calcutta");
  2. function dt_differ($start, $end){
  3. $start = date("G:i:s:m:d:Y", strtotime($start));
  4. $date1=explode(":", $start);
  5. $end = date("G:i:s:m:d:Y", strtotime($end));
  6. $date2=explode(":", $end);
  7. $starttime = mktime(date($date1[0]),date($date1[1]),date($date1[2]),
  8. date($date1[3]),date($date1[4]),date($date1[5]));
  9. $endtime = mktime(date($date2[0]),date($date2[1]),date($date2[2]),
  10. date($date2[3]),date($date2[4]),date($date2[5]));
  11. $seconds_dif = $starttime-$endtime;
  12. return $seconds_dif;
  13. }
复制代码
  1. function seconds2days($mysec) {
  2. $mysec = (int)$mysec;
  3. if ( $mysec === 0 ) {
  4. return '0 second';
  5. }
  6. $mins = 0;
  7. $hours = 0;
  8. $days = 0;
  9. if ( $mysec >= 60 ) {
  10. $mins = (int)($mysec / 60);
  11. $mysec = $mysec % 60;
  12. }
  13. if ( $mins >= 60 ) {
  14. $hours = (int)($mins / 60);
  15. $mins = $mins % 60;
  16. }
  17. if ( $hours >= 24 ) {
  18. $days = (int)($hours / 24);
  19. $hours = $hours % 60;
  20. }
  21. $output = '';
  22. if ($days){
  23. $output .= $days." days ";
  24. }
  25. if ($hours) {
  26. $output .= $hours." hours ";
  27. }
  28. if ( $mins ) {
  29. $output .= $mins." minutes ";
  30. }
  31. if ( $mysec ) {
  32. $output .= $mysec." seconds ";
  33. }
  34. $output = rtrim($output);
  35. return $output;
  36. }
复制代码
  1. $zip = zip_open("moooredale.zip");
  2. if ($zip) {
  3. while ($zip_entry = zip_read($zip)) {
  4. $fp = fopen(zip_entry_name($zip_entry), "w");
  5. if (zip_entry_open($zip, $zip_entry, "r")) {
  6. $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
  7. fwrite($fp,"$buf");
  8. zip_entry_close($zip_entry);
  9. fclose($fp);
  10. }
  11. }
  12. zip_close($zip);
  13. }
  14. ?>
复制代码


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:转换数组编码 Artikel seterusnya:得到文章中第一个图片的链接