PHP Snippets
« 1 2 3 4 5 » ... 11array_natcase
Information
(PHP Version >= 3)Functions used in this snippet: natcasesort, array_reverse.
Description
array array_natcase( str key, array array[, str ascdesc] )
Sorts the associative array array by the associative array key key with natcasesort. It maintains all keys and can be sorted ascending or descending by setting ascdesc to either asc, or desc. The function returns the newly array sorted array.
Snippet
<?php
function array_natcase($key, $array, $ascdesc='asc')
{
$temp = array();
$final = array();
foreach($array as $id=>$value)
{
$temp[$id] = $value[$key];
}
natcasesort($temp);
foreach($temp as $id=>$value)
{
$final[$id] = $array[$id];
}
if($ascdesc{0} === 'd')
{
$final = array_reverse($final, true);
}
return $final;
}
?>
Example
array_natcase Can be used in the following way:
<?php
$array = array(
'a'=>array('x'=>10, 'other'=>'egh'),
'q'=>array('x'=>9, 'other'=>'blah'),
'7'=>array('x'=>1, 'other'=>'rawr'),
'6'=>array('x'=>22, 'other'=>'abcd'));
$array = array_natcase('x', $array);
//outputs the array in the following order
// 7, q, a, 6
?>
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';
}
?>
image_constraints
Information
(PHP Version >= 4.0.6)Functions used in this snippet: imagesx, imagesy, imagecreatetruecolor, imagecopyresized.
Description
int image_constraints( int img, int dimx, int dimy )
Creates a resized image with the maximum width being dimx and height dimy without distorting the image.
Snippet
<?php
function image_constraints($img, $dimx, $dimy){
$x = imagesx($img);
$y = imagesy($img);
$xorg = $x;
$yorg = $y;
$altered = false;
if($x > $dimx){
$ratio = ($dimx / $x);
$x = $ratio * $x;
$y = $ratio * $y;
$altered = true;
}
if($y > $dimy){
$ratio = ($dimy / $y);
$y = $ratio * $y;
$x = $ratio * $x;
$altered = true;
}
if(!$altered){
return $img;
}
$dest = imagecreatetruecolor($x, $y);
imagecopyresized($dest, $img, 0, 0, 0, 0, $x, $y, $xorg, $yorg);
return $dest;
}
?>
Example
image_constraints Can be used in the following way:
<?php
$img = imagecreatefromjpeg('someimage.jpg'); // img size is 250 by 300 we will say
$img = image_constraints($img, 100, 100);
imagejpeg($img); // output image is 83 by 100
?>
link_track
Information
(PHP Version >= 4.3.0)Functions used in this snippet: file_exists, is_readable, is_writable, unserialize, file_get_contents, is_array, array_key_exists, isset, fopen, fwrite, serialize, fclose.
Description
mixed link_track( [ int id]] )
Keeps track of the number of times that clients have clicked a link. To find the number of clicks that a link has, leave id blank. To update a certain link, set id to the id of the link that you want to update.
Snippet
<?php
function link_track($id=NULL){
$file = 'somefile.txt'; // this is the file stuff is stored in
// make sure php can read and write to this file
if(!file_exists($file) || !is_readable($file) || !is_writable($file)){
return;
}
$links = array(0=>'http://www.bigtoach.com', 1=>'http://www.neverside.com', 2=>'http://www.google.com'); // change these to your links
$array = unserialize(file_get_contents($file));
$array = is_array($array) ? $array : array();
if($id !== NULL){
if(!array_key_exists($id, $links)){
return 'sorry there is no link to the id ' . $id;
}
$array[$id] = isset($array[$id]) ? $array[$id] + 1 : 1;
$fp = fopen($file, 'w');
fwrite($fp, serialize($array));
fclose($fp);
return $links[$id];
}
$ret = array();
foreach($links as $key=>$value){
$ret[$key]['site'] = $value;
$ret[$key]['clicks'] = isset($array[$key]) ? $array[$key] : 0;
$ret[$key]['id'] = $key;
}
return $ret;
}
?>
Example
link_track Can be used in the following way:
<?php
// this snippet is to find # of clicks
$array = link_track(); // gets the array
foreach($array as $key=>$value){
echo '<a href="pass.php?id=' . $value['id'] . '">' . $value['site'] . ' (' . $value['clicks'] . ' Clicks)</a><br />';
}
// you can also get click counts like this
echo '<a href="pass.php?id=0">http://www.bigtoach.com (' . $array[0]['clicks'] . ' Clicks)</a>';
// pass.php would look as the following
$_GET['id'] = isset($_GET['id']) ? intval($_GET['id']) : NULL;
$redirect = link_track($_GET['id']);
if(strstr($redirect, 'http')){
header('Location: ' . $redirect);
}else{
echo 'Sorry that link does not exist';
}
?>
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($phase, 0, 4);
$moon['phase'] = substr($phase, 4);
switch($moon['phase']){
case 'new':
$moon['current'] = 'Waning Crescent';
$moon['next'] = 'New Moon';
$moon['pct'] = abs(1 - (($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(1 - (($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
?>
« 1 2 3 4 5 » ... 11