BigToach.com

PHP Snippets

1 2 »

more_nums

Information

(PHP Version >= 3)
Functions used in this snippet: strlen, str_replace.

Description

bool more_nums( mixed str )

Returns true if the given string str contains more numbers than characters. Returns false if not.

Snippet

<?php
function more_nums($str){
    
$len strlen($str);
    
$str str_replace(array(0,1,2,3,4,5,6,7,8,9), ''$str);
    if( ( 
strlen($str) / $len ) < 0.5)
    {
        return 
true;
    }
    return 
false;
}
?>

Example

more_nums Can be used in the following way:

<?php
$str 
'abcd123';
if(
more_nums($str))
{
   echo 
'More Numbers';
}
else
{
   echo 
'Less Numbers';
}

$str 'ab12345cd6';
if(
more_nums($str))
{
   echo 
'More Numbers';
}
?>

Added on Oct 19th at 9 pm by Scott - 0 Comments
Updated on Oct 19th at 9 pm.


moon

Information

(PHP Version >= 3)
Functions used in this snippet: substr, abs.

Description

array moon([int now])

moon returns an array with information about the moon's phase. If the optional now variable is set, the information returned will be for the time that is given as the arguement.

The array that is returned has the following information.

num = The current lunar rotation (not used often)
phase = The phase the moon is currently moving towards(full, last, new, first)
current = The current sub phase (Waxing Gibbous, Waning Gibbous, Waning Crescent, Waxing Crescent)
next = Same as phase just formatted better (Full Moon, Last Quarter, New Moon, First Quarter)
pct = Percent of the visible moon that is illuminated (1.0 at full moon, 0.0 at new moon)
percent Percent of the illuminated moon visible (0.5 at full moon, 0.0 at new moon)
changes = The time that the moon changes from one phase to another (new moon to first quarter) this is a unixtimestamp in universal time


Note: This function only catalogs the moons phases until 2010

Snippet

<?php
function moon($now=NULL){
    if(
$now === NULL){
        
$now time();
    }
    
$phases = array(
        
'1015new'     => 1105387380,
        
'1015first'     => 1105973880,
        
'1015full'     => 1106677980,
        
'1015last'     => 1107358020,
        
'1016new'     => 1107930540,
        
'1016first'     => 1108541760,
        
'1016full'     => 1109249640,
        
'1016last'     => 1109900220,
        
'1017new'     => 1110474660,
        
'1017first'     => 1111116000,
        
'1017full'     => 1111813140,
        
'1017last'     => 1112431860,
        
'1018new'     => 1113017580,
        
'1018first'     => 1113687480,
        
'1018full'     => 1114362420,
        
'1018last'     => 1114953900,
        
'1019new'     => 1115567160,
        
'1019first'     => 1116259020,
        
'1019full'     => 1116904740,
        
'1019last'     => 1117478880,
        
'1020new'     => 1118120160,
        
'1020first'     => 1118823780,
        
'1020full'     => 1119438840,
        
'1020last'     => 1120008240,
        
'1021new'     => 1120676580,
        
'1021first'     => 1121379600,
        
'1021full'     => 1121968860,
        
'1021last'     => 1122546000,
        
'1022new'     => 1123236300,
        
'1022first'     => 1123925940,
        
'1022full'     => 1124499180,
        
'1022last'     => 1125094740,
        
'1023new'     => 1125798360,
        
'1023first'     => 1126463820,
        
'1023full'     => 1127034060,
        
'1023last'     => 1127655660,
        
'1024new'     => 1128360480,
        
'1024first'     => 1128996060,
        
'1024full'     => 1129576440,
        
'1024last'     => 1130228220,
        
'1025new'     => 1130923500,
        
'1025first'     => 1131530280,
        
'1025full'     => 1132131480,
        
'1025last'     => 1132812720,
        
'1026new'     => 1133478060,
        
'1026first'     => 1134063420,
        
'1026full'     => 1134692160,
        
'1026last'     => 1135395420,
        
'1027new'     => 1136027520,
        
'1027first'     => 1136602620,
        
'1027full'     => 1137260940,
        
'1027last'     => 1137971640,
        
'1028new'     => 1138572900,
        
'1028first'     => 1139149740,
        
'1028full'     => 1139834700,
        
'1028last'     => 1140535020,
        
'1029new'     => 1141115460,
        
'1029first'     => 1141704960,
        
'1029full'     => 1142408160,
        
'1029last'     => 1143083460,
        
'1030new'     => 1143656160,
        
'1030first'     => 1144263660,
        
'1030full'     => 1144971660,
        
'1030last'     => 1145615340,
        
'1031new'     => 1146192240,
        
'1031first'     => 1146831240,
        
'1031full'     => 1147528320,
        
'1031last'     => 1148142060,
        
'1032new'     => 1148732760,
        
'1032first'     => 1149401160,
        
'1032full'     => 1150074240,
        
'1032last'     => 1150664940,
        
'1033new'     => 1151276760,
        
'1033first'     => 1151969820,
        
'1033full'     => 1152612120,
        
'1033last'     => 1153188780,
        
'1034new'     => 1153827120,
        
'1034first'     => 1154533560,
        
'1034full'     => 1155146040,
        
'1034last'     => 1155718320,
        
'1035new'     => 1156385400,
        
'1035first'     => 1157090220,
        
'1035full'     => 1157679780,
        
'1035last'     => 1158257760,
        
'1036new'     => 1158950760,
        
'1036first'     => 1159639440,
        
'1036full'     => 1160215980,
        
'1036last'     => 1160810760,
        
'1037new'     => 1161519300,
        
'1037first'     => 1162185960,
        
'1037full'     => 1162760340,
        
'1037last'     => 1163382360,
        
'1038new'     => 1164089940,
        
'1038first'     => 1164724200,
        
'1038full'     => 1165307100,
        
'1038last'     => 1165962720,
        
'1039new'     => 1166652060,
        
'1039first'     => 1167259680,
        
'1039full'     => 1167861480,
        
'1039last'     => 1168548300,
        
'1040new'     => 1169208060,
        
'1040first'     => 1169794920,
        
'1040full'     => 1170423960,
        
'1040last'     => 1171129920,
        
'1041new'     => 1171757700,
        
'1041first'     => 1172332620,
        
'1041full'     => 1172992680,
        
'1041last'     => 1173700500,
        
'1042new'     => 1174300980,
        
'1042first'     => 1174875420,
        
'1042full'     => 1175559360,
        
'1042last'     => 1176253500,
        
'1043new'     => 1176835020,
        
'1043first'     => 1177421760,
        
'1043full'     => 1178125800,
        
'1043last'     => 1178796480,
        
'1044new'     => 1179368880,
        
'1044first'     => 1179979380,
        
'1044full'     => 1180685040,
        
'1044last'     => 1181328180,
        
'1045new'     => 1181902440,
        
'1045first'     => 1182543360,
        
'1045full'     => 1183236540,
        
'1045last'     => 1183852440,
        
'1046new'     => 1184439840,
        
'1046first'     => 1185111000,
        
'1046full'     => 1185781680,
        
'1046last'     => 1186374000,
        
'1047new'     => 1186984980,
        
'1047first'     => 1187679300,
        
'1047full'     => 1188322560,
        
'1047last'     => 1188898380,
        
'1048new'     => 1189539900,
        
'1048first'     => 1190245680,
        
'1048full'     => 1190861160,
        
'1048last'     => 1191431220,
        
'1049new'     => 1192104060,
        
'1049first'     => 1192808040,
        
'1049full'     => 1193399520,
        
'1049last'     => 1193980740,
        
'1050new'     => 1194678240,
        
'1050first'     => 1195367580,
        
'1050full'     => 1195943400,
        
'1050last'     => 1196541900,
        
'1051new'     => 1197250860,
        
'1051first'     => 1197915480,
        
'1051full'     => 1198487760,
        
'1051last'     => 1199116260,
        
'1052new'     => 1199821080,
        
'1052first'     => 1200455160,
        
'1052full'     => 1201037700,
        
'1052last'     => 1201698180,
        
'1053new'     => 1202384700,
        
'1053first'     => 1202988840,
        
'1053full'     => 1203593460,
        
'1053last'     => 1204280340,
        
'1054new'     => 1204938900,
        
'1054first'     => 1205520360,
        
'1054full'     => 1206153660,
        
'1054last'     => 1206856080,
        
'1055new'     => 1207479360,
        
'1055first'     => 1208050380,
        
'1055full'     => 1208712360,
        
'1055last'     => 1209417180,
        
'1056new'     => 1210015140,
        
'1056first'     => 1210589280,
        
'1056full'     => 1211274720,
        
'1056last'     => 1211968620,
        
'1057new'     => 1212546180,
        
'1057first'     => 1213135440,
        
'1057full'     => 1213835460,
        
'1057last'     => 1214507400,
        
'1058new'     => 1215076740,
        
'1058first'     => 1215689700,
        
'1058full'     => 1216393200,
        
'1058last'     => 1217036520,
        
'1059new'     => 1217610780,
        
'1059first'     => 1218252060,
        
'1059full'     => 1218946620,
        
'1059last'     => 1219560600,
        
'1060new'     => 1220151540,
        
'1060first'     => 1220821500,
        
'1060full'     => 1221495240,
        
'1060last'     => 1222085100,
        
'1061new'     => 1222701180,
        
'1061first'     => 1223395500,
        
'1061full'     => 1224039780,
        
'1061last'     => 1224615300,
        
'1062new'     => 1225264500,
        
'1062first'     => 1225973040,
        
'1062full'     => 1226585880,
        
'1062last'     => 1227159120,
        
'1063new'     => 1227833700,
        
'1063first'     => 1228541160,
        
'1063full'     => 1229128680,
        
'1063last'     => 1229711400,
        
'1064new'     => 1230409380,
        
'1064first'     => 1231099020,
        
'1064full'     => 1231673220,
        
'1064last'     => 1232275560,
        
'1065new'     => 1232985360,
        
'1065first'     => 1233645240,
        
'1065full'     => 1234219800,
        
'1065last'     => 1234849080,
        
'1066new'     => 1235554560,
        
'1066first'     => 1236181560,
        
'1066full'     => 1236767880,
        
'1066last'     => 1237427280,
        
'1067new'     => 1238112420,
        
'1067first'     => 1238711640,
        
'1067full'     => 1239314160,
        
'1067last'     => 1240000620,
        
'1068new'     => 1240654980,
        
'1068first'     => 1241235900,
        
'1068full'     => 1241866920,
        
'1068last'     => 1242570420,
        
'1069new'     => 1243192320,
        
'1069first'     => 1243765380,
        
'1069full'     => 1244423520,
        
'1069last'     => 1245129300,
        
'1070new'     => 1245724560,
        
'1070first'     => 1246300140,
        
'1070full'     => 1246983720,
        
'1070last'     => 1247676840,
        
'1071new'     => 1248255300,
        
'1071first'     => 1248843600,
        
'1071full'     => 1249545300,
        
'1071last'     => 1250214960,
        
'1072new'     => 1250787720,
        
'1072first'     => 1251398520,
        
'1072full'     => 1252105380,
        
'1072last'     => 1252746960,
        
'1073new'     => 1253324700,
        
'1073first'     => 1253965800,
        
'1073full'     => 1254661860,
        
'1073last'     => 1255276560,
        
'1074new'     => 1255869240,
        
'1074first'     => 1256546580,
        
'1074full'     => 1257218040,
        
'1074last'     => 1257810960,
        
'1075new'     => 1258427640,
        
'1075first'     => 1259127600,
        
'1075full'     => 1259767860,
        
'1075last'     => 1260346440,
        
'1076new'     => 1260993780,
        
'1076first'     => 1261705020,
        
'1076full'     => 1262315580,
        
'1076last'     => 1262889600,
        
'1077new'     => 1263568320,
        
'1077first'     => 1264272840,
        
'1077full'     => 1264861080,
        
'1077last'     => 1265442540,
        
'1078new'     => 1266144720,
        
'1078first'     => 1266828180,
        
'1078full'     => 1267403880,
        
'1078last'     => 1268005380,
        
'1079new'     => 1268715720,
        
'1079first'     => 1269370860,
        
'1079full'     => 1269944760,
        
'1079last'     => 1270571880,
        
'1080new'     => 1271273400,
        
'1080first'     => 1271899200,
        
'1080full'     => 1272482340,
        
'1080last'     => 1273144500,
        
'1081new'     => 1273824300,
        
'1081first'     => 1274424180,
        
'1081full'     => 1275026880,
        
'1081last'     => 1275714840,
        
'1082new'     => 1276366500,
        
'1082first'     => 1276947000,
        
'1082full'     => 1277577060,
        
'1082last'     => 1278279360,
        
'1083new'     => 1278902460,
        
'1083first'     => 1279473060,
        
'1083full'     => 1280133420,
        
'1083last'     => 1280836740,
        
'1084new'     => 1281434940,
        
'1084first'     => 1282007700,
        
'1084full'     => 1282694700,
        
'1084last'     => 1283386920,
        
'1085new'     => 1283967000,
        
'1085first'     => 1284555000,
        
'1085full'     => 1285258680,
        
'1085last'     => 1285930380,
        
'1086new'     => 1286502300,
        
'1086first'     => 1287116880,
        
'1086full'     => 1287823020,
        
'1086last'     => 1288467960,
        
'1087new'     => 1289047920,
        
'1087first'     => 1289695140,
        
'1087full'     => 1290389280,
        
'1087last'     => 1291005420,
        
'1088new'     => 1291599360,
        
'1088first'     => 1292277540,
        
'1088full'     => 1292948040,
        
'1088last'     => 1293538740);
    foreach(
$phases as $phase=>$time){
        if(
$now $time){
            break;
        }
    }
    
//$num   = substr($phase, 0, 4);
    //$phase = substr($phase, 4);
    
$data = array();
    
$moon['num']   = substr($phase04);
    
$moon['phase'] = substr($phase4);
    switch(
$moon['phase']){
        case 
'new':
            
$moon['current'] = 'Waning Crescent';
            
$moon['next'] = 'New Moon';
            
$moon['pct']  = abs(- (($now $phases[($moon['num'] - 1) . 'last']) / ($time $phases[($moon['num'] - 1) . 'last']))) * .5;
        break;
        case 
'first':
            
$moon['current'] = 'Waxing Crescent';
            
$moon['next'] = 'First Quarter';
            
$moon['pct']  = ($now $phases[$moon['num'] . 'new']) / ($time $phases[$moon['num'] . 'new']) * .5;
        break;
        case 
'full':
            
$moon['current'] = 'Waxing Gibbous';
            
$moon['next'] = 'Full Moon';
            
$moon['pct']  = ((($now $phases[$moon['num'] . 'first']) / ($time $phases[$moon['num'] . 'first'])) * .5) + .5;
        break;
        case 
'last':
            
$moon['current'] = 'Waning Gibbous';
            
$moon['next'] = 'Last Quarter';
            
$moon['pct'] = (abs(- (($now $phases[$moon['num'] . 'full']) / ($time $phases[$moon['num'] . 'full']))) * .5) + .5;
        break;
    }
    
$moon['percent'] = $moon['pct'] / 2;
    
$moon['changes'] = $time;
    return 
$moon;
}
?>

Example

moon Can be used in the following way:

<?php
$moon 
moon();
echo 
'You can currently see ' ceil($moon['percent'] * 100) . '% of the moon. It is currently a ' $moon['current'] . ' and is moving towards a ' $moon['next'] . ' and will get there on ' date('r'$moon['time']);
// would say something like
// You can currently see 10% of the moon. It is 
//currently a Waxing Crescent and is moving towards 
//a First Quarter and will get there on (some date).


$moon moon(strtotime('November 5 2006'));
// returns the moons info for november 5 2006 00:00
?>

Added on Aug 14th at 7 am by Scott - 0 Comments


gmail_size

Information

(PHP Version >= 3)
Functions used in this snippet: time, substr, microtime, sizeof.

Description

int gmail_size( void )

Shows the current size that Google offers for its Gmail storage.

Note: It will only work until August 2008, assuming that Google continues to up its storage by 100 megs each month.

Snippet

<?php
function gmail_size(){
    
$sizes = array(    array(11202012000002350),
            array(
11228796000002450),
            array(
11255580000002550),
            array(
11281500000002650),
            array(
11308320000002750),
            array(
11334240000002850),                array(11361024000002950),
            array(
11387808000003050),
            array(
11412000000003150),                array(11438784000003250),
            array(
11464668000003350),
            array(
11491452000003450),                array(11517372000003550),
            array(
11544156000003650),
            array(
11570940000003750),                array(11596860000003850),
            array(
11623680000003950),
            array(
11649600000004050),                array(11676384000004150),
            array(
11703168000004250),
            array(
11727360000004350),                array(11754144000004450),
            array(
11780028000004550),
            array(
11806812000004650),                array(11832732000004750),
            array(
11859516000004850),
            array(
11886300000004950),                array(11912220000005050),
            array(
11939040000005150),
            array(
11964960000005250),                array(11991744000005350),
            array(
12018528000005450),
            array(
12043584000005550),                array(12070368000005650),
            array(
12096252000005750),
            array(
12123036000005850),                array(12148956000005950),
            array(
12175740000006050),
            array(
12202524000006150));
    
$now time() . substr(microtime(), 23);
    for(
$i=0$i<sizeof($sizes); $i++){
        if(
$now $sizes[$i][0]){
            break;
        }
    }
    if(
$i == 0){
        
$ret $sizes[0][1];
    }elseif(
$i == sizeof($sizes)){
        
$ret $sizes[$i 1][1];
    }else{
        
$ts $sizes[$i 1][0];
        
$bs $sizes[$i 1][1];
        
$ret = (($now $ts) / ($sizes[$i][0] - $ts) * ($sizes[$i][1] - $bs)) + $bs;
    }
    return 
$ret;
}
?>

Example

gmail_size Can be used in the following way:

<?php
echo gmail_size(); // returns 2479.784683

// it will be different every time you see it

?>

Added on Aug 10th at 8 am by Scott - 0 Comments


letterfy

Information

(PHP Version >= 4)
Functions used in this snippet: is_numeric, strlen, in_array, substr.

Description

str letterfy( int int )

Returns a [typestr[/type] suffix of either st, nd, rd, or th based on the last 1 or two digits of the supplied int

Snippet

<?php
function letterfy($int){
    if(!
is_numeric($int{strlen($int) - 1})) return;
    if(
in_array(substr($int,-2), array(11,12,13))) return 'th';
    switch(
$int{strlen($int) - 1}){
        case 
1: return 'st';
        case 
2: return 'nd';
        case 
3: return 'rd';
        default: return 
'th';
    }
}
?>

Example

letterfy Can be used in the following way:

<?php
$x 
10;
echo 
$x letterfy($x); // 10th

$x 1;
echo 
$x letterfy($x// 1st

$x 22;
echo 
$x letterfy($x// 22nd

$x 33;
echo 
$x letterfy($x// 33rd

$x 111;
echo 
$x letterfy($x// 111th


?>

Added on Aug 10th at 7 am by Scott - 0 Comments


avg

Information

(PHP Version >= 4.0.4)
Functions used in this snippet: is_array, sizeof, array_sum.

Description

int avg( array array )

Takes the array array and gives you the average of its values.

Snippet

<?php
function avg($array){
    if(
is_array($array) && sizeof($array) !== 0){
        return (
array_sum($array) / sizeof($array));
    }
    return 
false;
}
?>

Example

avg Can be used in the following way:

<?php
$array 
= array(1,3,4,9,41,5,87,6,62,4,8,6,8,7,5,6);

echo 
avg($array); // 16.375
?>

Added on Aug 8th at 5 am by Scott - 0 Comments


1 2 »