Skip to main content

Posts

Showing posts from September, 2010

PHP Browser Detection

<?php $str = $_SERVER['HTTP_USER_AGENT']; $pos1 = strpos($str, "Chrome"); $pos2 = strpos($str, "Firefox"); $pos3 = strpos($str, "MSIE"); if($pos1 != '') $user_agent = "Chrome"; else if($pos2 != '') $user_agent = "Firefox"; else if($pos3 != '') $user_agent = "IE"; echo $user_agent; ?>

Validate Email

<?php function smcf_validate_email($email) { $at = strrpos($email, "@"); // Make sure the at (@) sybmol exists and // it is not the first or last character if ($at && ($at < 1 || ($at + 1) == strlen($email))) return false; // Make sure there aren't multiple periods together if (preg_match("/(\.{2,})/", $email)) return false; // Break up the local and domain portions $local = substr($email, 0, $at); $domain = substr($email, $at + 1); // echo $local.'<br>'; // echo $domain.'<br>'; // Check lengths $locLen = strlen($local); $domLen = strlen($domain); if ($locLen < 1 || $locLen > 64 || $domLen < 4 || $domLen > 255) return false; // Make sure local and domain don't start with or end with a period if (preg_match("/(^\.|\.$)/", $local) || preg_match("/(^\.|\.$)/", $domain)) return false; // Check for quoted-string addresses // Since almost anything is allowed in a quoted-

Calculate age, given date of birth

This class can calculate a person's age and also display values like the number of days and weeks lived. <?php class Age { var $age = ''; function calculateAge($iTimestamp) { $iDiffYear = date('Y') - date('Y', $iTimestamp); $iDiffMonth = date('n') - date('n', $iTimestamp); $iDiffDay = date('j') - date('j', $iTimestamp); // If birthday has not happen yet for this year, subtract 1. if ($iDiffMonth < 0 || ($iDiffMonth == 0 && $iDiffDay < 0)) { $iDiffYear--; } $this->age = $iDiffYear; } function getAge() { return $this->age; } function get_rank($rank) { $last = substr( $rank, -1 ); $seclast = substr( $rank, -2, -1 ); if( $last > 3 || $last == 0 ) $ext = 'th'; else if( $last == 3 ) $ext = 'rd'; else if( $last == 2 ) $ext = 'nd'; else $ext = 'st'; if( $last == 1 && $seclast == 1) $ext = 'th'; if( $last == 2 && $seclast == 1) $ext = 'th'; if( $l