<?php
/**
*
* @param
* @return the last day of the month
*/
function _getLastDayOfMonth($year, $month) {
$lastDay = idate('d', mktime(0, 0, 0, ((int)$month + 1), 0, (int)$year));
return $year . '-' . $month . '-' . $lastDay;
}
/**
* This function will usually be used with convert_smart_quotes() function.
* @param
* @return
*/
function str_convert_cp1252_utf8($str) {
$strEncode = mb_detect_encoding($str, 'UTF-8, ISO-8859-1');
if ($strEncode == 'ISO-8859-1') {
$str = mb_convert_encoding($str, 'UTF-8', 'CP1252');
}
return $str;
}
function convert_smart_quotes($string) {
// $search = array(chr(145),
// chr(146),
// chr(147),
// chr(148),
// chr(151));
$search = array(
'‘',
'’',
'“',
'”',
'–',
);
$replace = array(
"'",
"'",
'"',
'"',
'-',
);
return str_replace($search, $replace, $string);
}
/**
* This function checks for dates in a string.
* The string MUST be in one of 2 formats listed below in the examples.
* @param
* @return
* is_date('2008-02-01'); //Returns TRUE
* is_date('ssss 2008-02-01'); //Returns FALSE
* is_date('2008-02-01 02:33:10') //Returns TRUE
*/
function is_date($date) {
$len = strlen($date);
if ($len <= 10) {
return preg_match('/^[0-9]{4,4}-[0-9]{1,2}-[0-9]{1,2}$/', $date);
}
elseif ($len > 10 && $len <= 19) {
return preg_match('/^[0-9]{4,4}-[0-9]{1,2}-[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}$/', $date);
}
else {
return 0;
}
}
/**
* Parse a CSV string into an array ().
* The build-in fgetcsv() function required you to specify a file pointer.
* This function used memory instead of a file pointer.
* @param
* @return
*/
function str_getcsv_mem($input, $delimiter = ",", $enclosure = '"', $escape = "\\") {
$outputArr = Null;
$oneHundredKBs = 1 * 100 * 1024;
$fp = fopen('php://temp/maxmemory:' . $oneHundredKBs, 'r+');
fputs($fp, $input);
rewind($fp);
$rowCount = 0;
while (($row = fgetcsv($fp, 1000, $delimiter, $enclosure)) !== FALSE) { //$escape only got added in 5.3.0
foreach ($row as $key => $col) {
$outputArr[$rowCount][$key] = $col;
}
$rowCount++;
}
fclose($fp);
return $outputArr;
}
/**
* eat up multiple spaces (replace space) to single space
* @param
* @return
*/
function _eatSpaces($str) {
return ereg_replace( ' +', ' ', $str );
}
/**
* Split a string to an array by an array of positions.
* @param
* @return
*
* Sample:
* $pos = array(0, 1, 3, 6, 10);
* $str = "abbcccddddeeeee";
* $arr = split_str_by_pos($str, $pos);
*/
function split_str_by_pos($str, $pos) {
$arr = array();
$count = count($pos);
$leng = strlen($str);
for ($i=0;$i<$count;$i++) {
$start = $pos[$i];
$end = ($i == $count - 1) ? $leng : $pos[$i+1];
$fieldVal = '';
for ($j = $start; $j < $end; $j++) {
$fieldVal .= $str[$j];
}
$arr[] = $fieldVal;
}
return $arr;
}
/**
* Return duplicate values.
* @param
* @return
*/
function array_duplicates($arr) {
if (!is_array($arr)) {
return false;
}
$duplicates = array();
sort($arr);
$count = count($arr) - 1; // ### last index won't have duplicate.
for ($i = 0; $i < $count; $i++) {
if ($arr[$i] == $arr[$i+1]) {
$duplicates[] = $arr[$i];
$i++; // ### Skip next index.
}
}
return $duplicates;
}
/**
* Check if a number is a odd number. 單數
* @param
* @return help text for the path
*/
function is_odd($var)
{
return($var & 1);
}
/**
* Check if a number is a even number. 複數
* @param
* @return help text for the path
*/
function is_even($var)
{
return(!($var & 1));
}
/**
* Generate a random alphanumeric password.
*/
function random_password($length = 10) {
// This variable contains the list of allowable characters for the
// password. Note that the number 0 and the letter 'O' have been
// removed to avoid confusion between the two. The same is true
// of 'I', 1, and 'l'.
$allowable_characters = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
// Zero-based count of characters in the allowable list:
$len = strlen($allowable_characters) - 1;
// Declare the password as a blank string.
$pass = '';
// Loop the number of times specified by $length.
for ($i = 0; $i < $length; $i++) {
// Each iteration, pick a random character from the
// allowable string and append it to the password:
$pass .= $allowable_characters[mt_rand(0, $len)];
}
return $pass;
}
/**
* The built-in in_array was not what I wanted. So, I make this function.
* @param
* @return help text for the path
*/
function in_array2($searchArr, $beSearchedArr) {
$searchArr = is_array($searchArr) ? $searchArr : array($searchArr);
foreach ($searchArr as $search) {
foreach ($beSearchedArr as $beSearched) {
if (strcmp($search, $beSearched) == 0) {
return true;
}
}
}
return false;
}
/**
*
* @param $fieldArr = array('field_name' => 'field_value');
* @return help text for the path
*/
function sql_rewrite_insert($tableName, $fieldArr) {
$fieldStr = '';
$fieldStr2 = '';
foreach ($fieldArr as $key => $val) {
$fieldStr .= $key . ',';
$fieldStr2 .= is_null($val) ? 'NULL,' : "'" . $val . "',";
}
// ### get rid of the last comma.
$fieldStr = mb_substr($fieldStr, 0, mb_strlen($fieldStr) - 1, 'UTF-8');
$fieldStr2 = mb_substr($fieldStr2, 0, mb_strlen($fieldStr2) - 1, 'UTF-8');
$output = 'INSERT INTO ' . $tableName . ' (' . $fieldStr . ') VALUES (' . $fieldStr2 . ');';
return $output;
}
/**
*
* @param $fieldArr = array('field_name' => 'field_value');
* @param $whereFieldArr = array('field_name' => 'field_value');
* @return help text for the path
*/
function sql_rewrite_update($tableName, $fieldArr, $whereFieldArr = Null) {
$fieldStr = '';
foreach ($fieldArr as $key => $val) {
$fieldStr .= $key . " = '" . $val . "',";
}
$fieldStr = mb_substr($fieldStr, 0, -1, 'UTF-8');
$sql = 'UPDATE ' . $tableName . ' ';
$sql .= 'SET ';
$sql .= $fieldStr . ' ';
$sql .= "WHERE 1=1 ";
if (is_array($whereFieldArr)) {
foreach ($whereFieldArr as $key => $val) {
$sql .= "AND " . $key . " = '" . $val . "' ";
}
}
return $sql;
}
/**
* Write data to a file.
* @param $silence: to output warning message or not.
* @return help text for the path
*/
function writeData($fileName, $content, $mode, $silence = false) {
// Let's make sure the file exists and is writable first.
if (is_writable($fileName)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($fileName, $mode)) {
echo 'Cannot open file (' . $fileName . ')';
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $content) === FALSE) {
if (!$silence) {
echo 'Cannot write to file (' . $fileName . ')';
}
exit;
}
if (!$silence) {
echo 'Success, wrote content to file (' . $fileName . ')';
}
fclose($handle);
}
else {
if (!$silence) {
echo 'The file '. $fileName . ' is not writable';
}
}
}
function _getExchangeID2( $exchgCodeL ) {
### V TSXV 1
### T TSX 2
### N NYSE 3
### Q,QM,QS NASDAQ 4
### A AMEX 5
### QB OTCBB 8
switch (strtoupper($exchgCodeL)) {
case 'TSXV':
$exchg = '1';
break;
case 'TSX':
$exchg = '2';
break;
case 'NYSE':
$exchg = '3';
break;
case 'NASDAQ':
$exchg = '4';
break;
case 'AMEX':
$exchg = '5';
break;
case 'OTCBB':
$exchg = '8';
break;
case 'OTCBB PK':
$exchg = '16';
break;
case 'CNQ':
$exchg = '17';
break;
default:
$exchg = 'All Exchanges';
}
return $exchg;
}
function _getExchangeCodeL2( $exchgID ) {
### V TSXV 1
### T TSX 2
### N NYSE 3
### Q,QM,QS NASDAQ 4
### A AMEX 5
### QB OTCBB 8
switch ($exchgID) {
case 1:
$exchg = 'TSXV';
break;
case 2:
$exchg = 'TSX';
break;
case 3:
$exchg = 'NYSE';
break;
case 4:
$exchg = 'NASDAQ';
break;
case 5:
$exchg = 'AMEX';
break;
case 8:
$exchg = 'OTCBB';
break;
case 16:
$exchg = 'OTCBB PK';
break;
case 17:
$exchg = 'CNQ';
break;
default:
$exchg = 'All Exchanges';
}
return $exchg;
}
/**
* @param $stockChange = array('up' => '', 'down' => '', 'nochange' => '');
* @param $outputFormat can be 'file', 'browser'.
* @return
*/
function stock_change_chart($stockChange = NULL, $fileName, $outputFormat = 'file') {
if (is_array($stockChange)) {
$total = $stockChange['up'] + $stockChange['down'] + $stockChange['nochange'];
$aa = 0;
$bb = ($stockChange['up'] / $total) * 360;
$cc = $bb + ($stockChange['down'] / $total) * 360;
$dd = 360;
// create image
$image = imagecreatetruecolor(100, 100);
// ### For *transparent* PNG image
imagesavealpha($image, true);
$trans_colour = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $trans_colour);
// allocate some solors
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$gray = imagecolorallocate($image, 0xCC, 0xCC, 0xCC);
$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
$red = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);
$green = imagecolorallocate($image, 0x33, 0x99, 0x00);
$darkgreen = imagecolorallocate($image, 0x33, 0x66, 0x00);
// make the 3D effect
for ($i = 60; $i > 50; $i--) {
imagefilledarc($image, 50, $i, 100, 50, $aa, $bb, $darkgreen, IMG_ARC_PIE);
imagefilledarc($image, 50, $i, 100, 50, $bb, $cc , $darkred, IMG_ARC_PIE);
imagefilledarc($image, 50, $i, 100, 50, $cc, $dd , $darkgray, IMG_ARC_PIE);
}
imagefilledarc($image, 50, 50, 100, 50, $aa, $bb, $green, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, $bb, $cc , $red, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, $cc, $dd , $gray, IMG_ARC_PIE);
### flush image
// if you want to output to browser, uncomment following line, and remove the file name from next line.
//header('Content-type: image/png');
imagepng($image, $fileName);
imagedestroy($image);
}
}
?>
Monday, April 20, 2009
danny_library.php
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment