Search:

Categories



CSV to array

function csv_to_array($filename, $delimiter) {
    ini_set('auto_detect_line_endings', true);

    if(!file_exists($filename) || !is_readable($filename)) {
        return FALSE;
    }

    $header = NULL;
    $data = array();
    if (($handle = fopen($filename, 'r')) !== FALSE)   {
        while (($row = fgetcsv($handle, $delimiter)) !== FALSE)
        {
            if(!$header) {
                $header = $row;
            } else {
                $data[] = array_combine($header, $row);
            }
        }
        fclose($handle);
    }
    return $data;
}

List all active plugins

Lists all active plugins in wordpress, may be handy if you run a few sites

function wp_list_active_site_plugins() {
    $the_plugs = get_option('active_plugins'); 
    foreach($the_plugs as $key => $value) {
        $string = explode('/',$value); // Folder name will be displayed
        echo $string[0] ."\n";
    }
}

wp_list_active_site_plugins();

Convert Seconds to String

This function converts seconds to an easily readable string

<?php
function secondsToString($secs) 
{
	$r=0;
	if($secs>=86400){$days=floor($secs/86400);$secs=$secs%86400;$r=$days.' day';if($days<>1){$r.='s';}if($secs>0){$r.=', ';}}
	if($secs>=3600){$hours=floor($secs/3600);$secs=$secs%3600;$r.=$hours.' hour';if($hours<>1){$r.='s';}if($secs>0){$r.=', ';}}
	if($secs>=60){$minutes=floor($secs/60);$secs=$secs%60;$r.=$minutes.' minute';if($minutes<>1){$r.='s';}if($secs>0){$r.=', ';}}
	$r.=$secs.' second';if($secs<>1){$r.='s';}
	return $r;
}

//1 day, 22 hours, 35 minutes, 10 seconds
echo secondsToString(167710);
//268 days, 26 minutes, 18 seconds
echo secondsToString(23156778);
//03 minutes, 55 seconds
echo secondsToString(235);


?>

Get zodiac star sign for a date

 <?php
function zodiac($DOB){
    $DOB = date("m-d", strtotime($DOB));
    list($month,$day) = explode("-",$DOB);
    if(($month == 3 || $month == 4) && ($day > 22 || $day < 21)){
        $zodiac = "Aries";
    }
    elseif(($month == 4 || $month == 5) && ($day > 22 || $day < 22)){
        $zodiac = "Taurus";
    }
    elseif(($month == 5 || $month == 6) && ($day > 23 || $day < 22)){
        $zodiac = "Gemini";
    }
    elseif(($month == 6 || $month == 7) && ($day > 23 || $day < 23)){
        $zodiac = "Cancer";
    }
    elseif(($month == 7 || $month == 8) && ($day > 24 || $day < 22)){
        $zodiac = "Leo";
    }
    elseif(($month == 8 || $month == 9) && ($day > 23 || $day < 24)){
        $zodiac = "Virgo";
    }
    elseif(($month == 9 || $month == 10) && ($day > 25 || $day < 24)){
        $zodiac = "Libra";
    }
    elseif(($month == 10 || $month == 11) && ($day > 25 || $day < 23)){
        $zodiac = "Scorpio";
    }
    elseif(($month == 11 || $month == 12) && ($day > 24 || $day < 23)){
        $zodiac = "Sagittarius";
    }
    elseif(($month == 12 || $month == 1) && ($day > 24 || $day < 21)){
        $zodiac = "Cpricorn";
    }
    elseif(($month == 1 || $month == 2) && ($day > 22 || $day < 20)){
        $zodiac = "Aquarius";
    }
    elseif(($month == 2 || $month == 3) && ($day > 21 || $day < 21)){
        $zodiac = "Pisces";
    }

    return $zodiac;
}
//usage example
echo zodiac('1976-12-06'); //Valid strtotime date
echo "<br>";
echo zodiac('1986-04-04');
?> 

Whois in PHP

This is a basic whois example

A good list of whois servers can be found at http://www.nirsoft.net/whois_servers_list.html

<?php
function whois_query($domain) {

    // fix the domain name:
    $domain = strtolower(trim($domain));
    $domain = preg_replace('/^http:\/\//i', '', $domain);
    $domain = preg_replace('/^www\./i', '', $domain);
    $domain = explode('/', $domain);
    $domain = trim($domain[0]);

    // split the TLD from domain name
    $_domain = explode('.', $domain);
    $lst = count($_domain)-1;
    $ext = $_domain[$lst];

    // You find resources and lists 
    // like these on wikipedia: 
    //
    // http://de.wikipedia.org/wiki/Whois
    //
    $servers = array(
        "biz" => "whois.neulevel.biz",
        "com" => "whois.verisign-grs.com",
        "us" => "whois.nic.us",
        "coop" => "whois.nic.coop",
        "info" => "whois.nic.info",
        "name" => "whois.nic.name",
        "net" => "whois.internic.net",
        "gov" => "whois.nic.gov",
        "edu" => "whois.internic.net",
        "mil" => "rs.internic.net",
        "int" => "whois.iana.org",
        "ac" => "whois.nic.ac",
        "ae" => "whois.uaenic.ae",
        "at" => "whois.ripe.net",
        "au" => "whois.aunic.net",
        "be" => "whois.dns.be",
        "bg" => "whois.ripe.net",
        "br" => "whois.registro.br",
        "bz" => "whois.belizenic.bz",
        "ca" => "whois.cira.ca",
        "cc" => "whois.nic.cc",
        "ch" => "whois.nic.ch",
        "cl" => "whois.nic.cl",
        "cn" => "whois.cnnic.net.cn",
        "cz" => "whois.nic.cz",
        "de" => "whois.nic.de",
        "fr" => "whois.nic.fr",
        "hu" => "whois.nic.hu",
        "ie" => "whois.domainregistry.ie",
        "il" => "whois.isoc.org.il",
        "in" => "whois.ncst.ernet.in",
        "ir" => "whois.nic.ir",
        "mc" => "whois.ripe.net",
        "to" => "whois.tonic.to",
        "tv" => "whois.tv",
        "ru" => "whois.ripn.net",
        "org" => "whois.pir.org",
        "aero" => "whois.information.aero",
        "nl" => "whois.domain-registry.nl"
    );

    if (!isset($servers[$ext])){
        die('Error: No matching nic server found!');
    }

    $nic_server = $servers[$ext];

    $output = '';

    // connect to whois server:
    if ($conn = fsockopen ($nic_server, 43)) {
        fputs($conn, $domain."\r\n");
        while(!feof($conn)) {
            $output .= fgets($conn,128);
        }
        fclose($conn);
    }
    else { die('Error: Could not connect to ' . $nic_server . '!'); }

    return $output;
}
//usage example
echo whois_query("http://www.yahoo.com");
echo "<br>";
echo whois_query("http://www.yahoo.net");
?>

Seconds to string

submitted by PHPhacker

<?php
function secondsToString($secs) 
{
    if($secs>=86400){$days=floor($secs/86400);$secs=$secs%86400;$r=$days.' day';if($days<>1){$r.='s';}if($secs>0){$r.=', ';}}
    if($secs>=3600){$hours=floor($secs/3600);$secs=$secs%3600;$r.=$hours.' hour';if($hours<>1){$r.='s';}if($secs>0){$r.=', ';}}
    if($secs>=60){$minutes=floor($secs/60);$secs=$secs%60;$r.=$minutes.' minute';if($minutes<>1){$r.='s';}if($secs>0){$r.=', ';}}
    $r.=$secs.' second';if($secs<>1){$r.='s';}
    return $r;
}

//usage examples
echo secondsToString(5237);
echo "<br>";
echo secondsToString(88238);
?>