Wednesday, May 6, 2009

PHP Tips and Tricks


  • Generate random password



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <?php
     


    function generatePassword($length) {
    $character = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $password = "";
    for($i=0;$i<$length;$i++) {
    $password .= $character[rand(0, 61)];
    }
    return $password;
    }
     
    echo generatePassword(7);
     
    ?>




  • Calculate age



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?php
     
    $testcase = array("12-03-1985", "15-01-1987", "01-01-2000");
     
    function getAge($year) {
    return floor(abs(strtotime('now') - strtotime($year))/31536000);
    }
     
    foreach($testcase as $test) {
    echo "Born in $test, approximately age is " . getAge($test)." years<br/>";
    }
    ?>




  • Get visitor’s IP address



    1
    2
    3
    4
    5
    <?php
     
    echo "Your IP address is " . $_SERVER['REMOTE_ADDR'];
     
    ?>




  • Check even or add number



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <?php
     
    $testcase = array(1, 4, 5, 6, 11);
     
    function isOdd($number) {
    if ($number % 2 == 0) {
    return false;
    }
    return true;
    }
     
    foreach($testcase as $test) {
    if (isOdd($test)) {
    echo $test . " is odd<br/>";
    } else {
    echo $test . " is even<br/>";
    }
    }
     
    ?>




  • Get file extension



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <?php
     
    $testcase = array("sample.txt", "sample.jpg", "sample.case.txt");
     
    function extension($filename){
    return substr(strrchr($filename, '.'), 1);
    }
     
    foreach($testcase as $test) {
    echo "Extension from $test is " . extension($test) . "<br/>";
    }
     
    ?>




  • Sort data in array



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <?php
     
    $data_list = array("lychee", "pineapple", "apple", "mango", "strawberry", "banana", "orange", "grape", "guava");
     
    sort($data_list);
    foreach($data_list as $data) {
    echo $data . "<br/>";
    }
     
    ?>




  • Random pick from array



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <?php
     
    $testcase = array("sample.txt", "sample.jpg", "sample.case.txt");
     
    function extension($filename){
    return substr(strrchr($filename, '.'), 1);
    }
     
    foreach($testcase as $test) {
    echo "Extension from $test is " . extension($test) . "<br/>";
    }
     
    ?>




  • Shuffle data



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <?php
     
    $data_list = array("lychee", "pineapple", "apple", "mango", "strawberry", "banana", "orange", "grape", "guava");
     
    shuffle($data_list);
    foreach($data_list as $data) {
    echo $data . "<br/>";
    }
     
    ?>




  • Highlight text



    1
    2
    3
    4
    5
    6
    7
    8
    9
    <?php
     
    $text = "Sample sentence from KomunitasWeb, regex has become popular in web programming. Now we learn regex. According to wikipedia, Regular exspanssions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interspanted by a regular exspanssion processor";
     
    $text = spang_replace("/\b(regex)\b/i", '<span style="background:#5fc9f6">\1</span>', $text);
     
    echo $text;
     
    ?>




  • Change to title case (first letter uppercase)



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?php
    $testcase = array("hello world", "Hello WORLD", "heLLO world");
     
    function titleCase($text) {
    return ucwords(strtolower($text));
    }
     
    foreach($testcase as $test) {
    echo titleCase($test)."<br/>";
    }
     
    ?>




  • Time since



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    <?php
     
    $testcase = array("12-03-1985", "15-01-2007", "04-04-2009", "01-01-2009", "30-03-2009", "07-04-2009", "07-04-2009 11:12");
     
    define("MINUTE", 60);
    define("HOUR", 3600); // 60 * 60
    define("DAY", 86400); // 60 * 60 * 24
    define("WEEK", 604800); // 60 * 60 * 24 * 7
    define("MONTH", 2592000); // 60 * 60 * 24 * 30
    define("YEAR", 31536000); // 60 * 60 * 24 * 365
     
    function timeSince($date) {
     
    $since = abs(strtotime('now') - strtotime($date));
     
    if($since > YEAR) {
    $year = floor($since / YEAR);
    return "more than $year year(s) ago";
    }
     
    if ($since > MONTH) {
    $month = floor($since / MONTH);
    return "about $month month(s) ago";
    }
     
    if ($since > WEEK) {
    $week = floor($since / WEEK);
    $day = floor(($since - ($week * WEEK)) / DAY);
    return "about $week week(s), and $day day(s) ago";
    }
     
    if ($since > DAY) {
    $day = floor($since / DAY);
    $hour = floor(($since - ($day * DAY)) / HOUR);
    return "about $day day(s), $hour hour(s) ago";
    }
     
    if ($since > HOUR) {
    $hour = floor($since / HOUR);
    $minute = floor(($since - ($hour * HOUR)) / MINUTE);
    return "about $hour hour(s), $minute minute(s) ago";
    }
     
    if ($since > MINUTE) {
    $minute = floor($since / MINUTE);
    return "$minute minute(s) ago";
    }
     
    return "under 1 minute ago";
     
    }
     
    foreach($testcase as $test) {
    echo "Time since $test is " . timeSince($test)."<br/>";
    }
     
    ?>
  • No comments: