ホームページ  >  記事  >  バックエンド開発  >  いくつかの便利な PHP コード スニペット

いくつかの便利な PHP コード スニペット

WBOY
WBOYオリジナル
2016-07-25 08:50:25907ブラウズ
  1. $host="localhost";
  2. $uname="データベース ユーザー名";
  3. $pass="データベース パスワード";
  4. $database = "データベース名";
  5. $connection=mysql_connect( $host,$uname,$pass)
  6. or die("データベース接続に失敗しました");
  7. $result=mysql_select_db($database)
  8. or die("データベースを選択できません");
  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トリム( $str );>
  8. }
  9. echo Words_limit($yourString, 50,'...');
  10. or
  11. echowords_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. }
复制代
  1. function age_from_dob($dob){
  2. $dob = strtotime($dob);
  3. $y = date('Y', $dob);
  4. if (($m = (date('m') - date('m', $dob))) < 0) {
  5. $y++;
  6. } elseif ($m == 0 && date('d') - date('d', $dob) <0) {
  7. $y++;
  8. }
  9. return date('Y') - $y;
  10. }
  11. echo age_from_dob('2005/04/19'); yyyy/mm/dd 形式の日付。
复制代码
  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("アジア/カルカッタ");
  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. $秒_差分 = $開始時間-$終了時間;
  12. 戻り $秒_差;
  13. }
复制幣
  1. function Seconds2days($mysec) {
  2. $mysec = (int)$mysec;
  3. if ( $mysec === 0 ) {
  4. return '0秒';
  5. }
  6. $mins = 0;
  7. $時間 = 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." 時間 ";
  27. }
  28. if ( $mins ) {
  29. $output .= $mins." 分 ";
  30. }
  31. if ( $mysec ) {
  32. $output .= $mysec." 秒 ";
  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. ?>
复制發


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。