Some very useful snippets of PHP code

Source: Internet
Author: User
<无详细内容>
    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 is selected");
    9. ?>
Copy Code
    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);
Copy Code
    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. "/>
Copy Code
    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 '); Date in YYYY/MM/DD format.
Copy Code
    1. Set cookies
    2. Setcookie ("name", ' Value ', Time () +3600*60*30);
    3. Show cookies
    4. if ($_cookie["name"]!= "") {
    5. $_session[' name '] = $_cookie["name"];
    6. }
Copy Code
    1. Method 1
    2. Echo substr (MD5 (UNIQID ()), 0, 8);
    3. Method 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 < $length; $i + +)
    11. $str. = $chars [rand (0, $max)];
    12. return $str;
    13. }
    14. Echo Rand_password (16);
Copy Code
    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. $date 1=explode (":", $start);
    5. $end = Date ("G:i:s:m:d:y", Strtotime ($end));
    6. $date 2=explode (":", $end);
    7. $starttime = mktime (Date ($date 1[0]), date ($date 1[1]), date ($date 1[2]),
    8. Date ($date 1[3]), date ($date 1[4]), date ($date 1[5]);
    9. $endtime = mktime (Date ($date 2[0]), date ($date 2[1]), date ($date 2[2]),
    10. Date ($date 2[3]), date ($date 2[4]), date ($date 2[5]);
    11. $seconds _dif = $starttime-$endtime;
    12. return $seconds _dif;
    13. }
Copy Code
  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. }
Copy Code
    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. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.