Archive for the ‘php’ Category

"; echo "localtime: ".date("H:i:s"); if($_SERVER['SERVER_ADDR'] == $us_ip) { putenv('TZ=US/Central'); } echo " US/Central time: ".date("H:i:s"); ?>
function is_valid_email($input) { preg_match("^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$", $input)) }
'register_globals', 'Short Tags' => 'short_open_tag', 'Display Errors' => 'display_errors', 'Magic Quotes GPC' => 'magic_quotes_gpc', 'Magic Quotes Runtime' => 'magic_quotes_runtime', 'Magic Quotes Sybase' => 'magic_quotes_sybase', ); $ve = phpversion(); $os = PHP_OS; $er = intval(error_reporting()); foreach ($flags as $n => $v) { $flags[$n] = (in_array(strtolower(ini_get($v)), $neg) ? 'Off' : 'On'); } $cli = (php_sapi_name() == 'cli'); $eol = "\n"; $gle = get_loaded_extensions(); $rows = array(); $le = ''; $wide = 4; $j = count($gle); $pad = $wide - $j % $wide; $len = max(array_map('strlen', $gle)); $func = create_function('$a', 'return str_pad($a, ' . intval($len) . ');'); $gle = array_map($func, $gle); for($i = 0; $i < $j; $i += $wide) { $le .= ' ' . implode(' ', array_slice($gle, $i, $wide)) . $eol; } $ec = array( 'E_STRICT' => 2048, 'E_ALL' => 2047, 'E_USER_NOTICE' => 1024, 'E_USER_WARNING' => 512, 'E_USER_ERROR' => 256, 'E_COMPILE_WARNING' => 128, 'E_COMPILE_ERROR' => 64, 'E_CORE_WARNING' => 32, 'E_CORE_ERROR' => 16, 'E_NOTICE' => 8, 'E_PARSE' => 4, 'E_WARNING' => 2, 'E_ERROR' => 1, ); $e = array(); $t = $er; foreach ($ec as $n => $v) { if (($t & $v) == $v) { $e[] = $n; $t ^= $v; } } if (ceil(count($ec) / 2) + 1 < count($e)) { $e2 = array(); foreach ($ec as $n => $v) { if (!in_array($n, $e) and $n != 'E_ALL') { $e2[] = $n; } } $er = $er . ' ((E_ALL | E_STRICT) ^ ' . implode(' ^ ', $e2) . '))'; } else { $er = $er . ' (' . implode(' | ', $e) . ')'; } if (!$cli) { echo '
', $eol;
}

echo 'PHP Version: ', $ve, $eol;
echo 'PHP OS: ', $os, $eol;
echo 'Error Reporting: ', $er, $eol;
foreach ($flags as $n => $v)
{
echo $n, ': ', $v, $eol;
}
echo 'Loaded Extensions:', $eol, $le, $eol;				
if (get_magic_quotes_gpc()) { $in = array(&$_GET, &$_POST, &$_COOKIE); while (list($k,$v) = each($in)) { foreach ($v as $key => $val) { if (!is_array($val)) { $in[$k][$key] = stripslashes($val); continue; } $in[] =& $in[$k][$key]; } } unset($in); }

This function will return the current time in seconds and microseconds. This is sometimes useful to track down the processing time of a script or other short lasting events.

function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); }

An example usage could look like this:

$start = microtime_float(); // your code here $took = round(microtime_float() - $start, 2); echo "took $took seconds.";
This php function calculates the age in years for a given birthdate in the format YYYY-MM-DD. function getAge($birthdate) { // get month, day, year $tmp = split('-', $birthdate); $month = (int)$tmp[1]; $day = (int) $tmp[2]; $year = (int) $tmp[0]; $current_day_of_year = 1 + date('z'); $user_day_of_year = 1 + date('z', mktime(1,1,1,$month, $day, $year)); $years = date('Y') - $year; // we calculate based on birth day, if we haven't reached user's birthday yet, // substract one year if ($current_day_of_year < $user_day_of_year) { $years--; } return $years; }
This function allows it to hide the email address within a link so it is not this easy to be grabbed by spiders. function mailto($address, $text) { $address_encode = ''; for ($x=0; $x < strlen($address); $x++) { if(preg_match('!\w!',$address[$x])) { $address_encode .= '%' . bin2hex($address[$x]); } else { $address_encode .= $address[$x]; } } $text_encode = ''; for ($x=0; $x < strlen($text); $x++) { $text_encode .= '&#x' . bin2hex($text[$x]).';'; } $mailto = "mailto:"; return ''.$text_encode.''; } echo mailto('my@mail.com','Mail me');
This little function cuts a string to a specified length while keeping the word boundaries under consideration. function cuttext($text, $chars = 100) { if (strlen($text) > $chars) { $newtext = substr($text, 0, $chars); return preg_replace ("{\s+\S+$}", "...", $newtext); } else { return $text; } }
This little php class lets you create random characters of a given length. class RandomGenerator { static $letters = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o', 'p','q','r','s','t','u','v','w','x','y','z'); static $digits = array('1','2','3','4','5','6','7','8','9','0'); public function genKey($length = '128') { $key = ''; $maxIndexLeters = count(self::$letters) - 1; $maxIndexDigits = count(self::$digits) - 1; for ($i= 0 ; $i < $length ; $i++) { $makeDigit = (boolean) mt_rand(0,1); if ($makeDigit) { $index = mt_rand(0, $maxIndexDigits); $key .= self::$digits[$index]; } else { $index = mt_rand(0, $maxIndexLeters); $makeUpperCase = (boolean) mt_rand(0,1); if ($makeUpperCase) { $key .= strtoupper(self::$letters[$index]); } else { $key .= self::$letters[$index]; } } } return $key; } } echo RandomGenerator::genKey();
vision22.net

Copyright © 2007