Created
May 29, 2015 09:10
-
-
Save wdmtech/c7dd650198b3b5462176 to your computer and use it in GitHub Desktop.
A load of useful PHP functions that should really be factored into a package or something at some point.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class FormatMe | |
* | |
* This is just my collection of helper functions, should probably put them in a package | |
* at some point due to their usefulness. | |
* | |
*/ | |
class FormatMe { | |
/** | |
* Convert seconds to human readable text. | |
* | |
* @source http://csl.name/php-secs-to-human-text/ | |
* @param $secs | |
* @return string | |
*/ | |
static public function fromSecondsToHuman($secs){ | |
$units = array( | |
"week" => 7*24*3600, | |
"day" => 24*3600, | |
"hr" => 3600, | |
"min" => 60, | |
//"second" => 1, | |
); | |
// specifically handle zero | |
if ( $secs < 60 ) return "0hr, 0mins"; | |
$s = ""; | |
foreach ( $units as $name => $divisor ) { | |
if ( $quot = intval($secs / $divisor) ) { | |
$s .= "$quot$name"; | |
$s .= (abs($quot) > 1 ? "s" : "") . ", "; | |
$secs -= $quot * $divisor; | |
} | |
} | |
return substr($s, 0, -2); | |
} | |
/** | |
* ----------------------------------------------------------------------------------------------------------------- | |
* Takes an input in bytes and returns it in a human readable format. | |
* ----------------------------------------------------------------------------------------------------------------- | |
* | |
* @source http://jeffreysambells.com/2012/10/25/human-readable-filesize-php | |
* @param int $bytes | |
* @param int $decimals | |
* @return string | |
*/ | |
static public function toHumanFileSize($bytes, $decimals = 2) { | |
$size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB'); | |
$factor = floor((strlen($bytes) - 1) / 3); | |
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor]; | |
} | |
/** | |
* ----------------------------------------------------------------------------------------------------------------- | |
* Takes a number and returns its ordinal suffix | |
* ----------------------------------------------------------------------------------------------------------------- | |
* | |
* @source https://stackoverflow.com/questions/3109978/php-display-number-with-ordinal-suffix/3110033#3110033 | |
* @param int $number | |
* @return string | |
*/ | |
static public function toOrdinalSuffix($number = null) { | |
$ends = array('th','st','nd','rd','th','th','th','th','th','th'); | |
if (($number %100) >= 11 && ($number%100) <= 13) | |
return $number. 'th'; | |
else | |
return $ends[$number % 10]; | |
} | |
/** | |
* ----------------------------------------------------------------------------------------------------------------- | |
* Takes an input number and returns it with the correct suffix for the decimal power | |
* ----------------------------------------------------------------------------------------------------------------- | |
* | |
* @param int $input | |
* @param int $precision | |
* @return string | |
*/ | |
static public function withSuffix($input, $precision = 3) | |
{ | |
$suffixes = array('', 'k', 'm', 'g', 't'); | |
$suffixIndex = 0; | |
while(abs($input) >= 1000 && $suffixIndex < sizeof($suffixes)) | |
{ | |
$suffixIndex++; | |
$input /= 1000; | |
} | |
return ( | |
$input > 0 | |
// precision of 3 decimal places | |
? floor($input * 1000) / 1000 | |
: ceil($input * 1000) / 1000 | |
) | |
. $suffixes[$suffixIndex]; | |
} | |
/** | |
* ----------------------------------------------------------------------------------------------------------------- | |
* Takes an integer input and returns the string representation of it (one = 1, thirty three = 33, etc) | |
* I think this has a bug in it... | |
* ----------------------------------------------------------------------------------------------------------------- | |
* | |
* @source http://stackoverflow.com/a/2112655/1225977 | |
* @param int $number | |
* @return string | |
*/ | |
static public function numbersToLetters($number) | |
{ | |
$result = array(); | |
$tens = floor($number / 10); | |
$units = $number % 10; | |
$words = array | |
( | |
'units' => array('', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eightteen', 'Nineteen'), | |
'tens' => array('', '', 'Twenty', 'Thirty', 'Fourty', 'Fifty', 'Sixty', 'Seventy', 'Eigthy', 'Ninety') | |
); | |
if ($tens < 2) | |
{ | |
$result[] = $words['units'][$tens * 10 + $units]; | |
} | |
else | |
{ | |
$result[] = $words['tens'][$tens]; | |
if ($units > 0) | |
{ | |
$result[count($result) - 1] .= '-' . $words['units'][$units]; | |
} | |
} | |
if (empty($result[0])) | |
{ | |
$result[0] = 'Zero'; | |
} | |
return trim(implode(' ', $result)); | |
} | |
/** | |
* ----------------------------------------------------------------------------------------------------------------- | |
* Takes an input and replaces text smiles with image equivolents | |
* ----------------------------------------------------------------------------------------------------------------- | |
* | |
* @param $string | |
* @return mixed | |
*/ | |
static public function emoticons($string) | |
{ | |
$iconDirectory = URL::to('assets/smiles'); | |
// Supported icons, this should be supplemented by the app config or something... | |
$icons = array( | |
'smile' => array( | |
'matches' => array( | |
':)', | |
':-)', | |
), | |
'replace' => '<img src="'. $iconDirectory .'/smile.png" alt="smile" class="smile icon_smile" />' | |
), | |
'grin' => array( | |
'matches' => array( | |
':D', | |
':-D', | |
), | |
'replace' => '<img src="'. $iconDirectory .'/grin.png" alt="grin" class="smile icon_grin" />' | |
), | |
'tongue' => array( | |
'matches' => array( | |
':p', | |
':-p', | |
':P', | |
':-P' | |
), | |
'replace' => '<img src="'. $iconDirectory .'/tongue.png" alt="tongue" class="smile icon_tongue" />' | |
), | |
'sad' => array( | |
'matches' => array( | |
':(', | |
':-(' | |
), | |
'replace' => '<img src="'. $iconDirectory .'/sad.png" alt="sad face" class="smile icon_sad" />' | |
), | |
'wink' => array( | |
'matches' => array( | |
';)', | |
';-)' | |
), | |
'replace' => '<img src="'. $iconDirectory .'/wink.png" alt="wink" class="smile icon_wink" />' | |
), | |
'confused' => array( | |
'matches' => array( | |
':s', | |
':-s', | |
';s', | |
';-s' | |
), | |
'replace' => '<img src="'. $iconDirectory .'/confused.png" alt="confused" class="smile icon_confused" />' | |
), | |
'kwai' => array( | |
'matches' => array( | |
':3', | |
':-3', | |
), | |
'replace' => '<img src="'. $iconDirectory .'/kwai.png" alt="kwai" class="smile icon_kwai" />' | |
), | |
'cool' => array( | |
'matches' => array( | |
'8)', | |
'8-)', | |
), | |
'replace' => '<img src="'. $iconDirectory .'/cool.png" alt="cool" class="smile icon_cool" />' | |
), | |
'cry' => array( | |
'matches' => array( | |
':\'(', | |
':\'-(', | |
), | |
'replace' => '<img src="'. $iconDirectory .'/cry.png" alt="cry" class="smile icon_cry" />' | |
), | |
); | |
// Loop over icons and replace by mask | |
// Does not match if at begining of string or end of a line with no space or nl | |
foreach($icons as $iconGroup) | |
{ | |
foreach ($iconGroup['matches'] as $mask) | |
{ | |
$icon = preg_quote($mask); | |
$string = preg_replace("~\s$icon\s~", $iconGroup['replace'], $string); | |
} | |
} | |
// Return input with smiles replaced with images | |
return $string; | |
} | |
static function w1250_to_utf8($text) { | |
// map based on: | |
// http://konfiguracja.c0.pl/iso02vscp1250en.html | |
// http://konfiguracja.c0.pl/webpl/index_en.html#examp | |
// http://www.htmlentities.com/html/entities/ | |
$map = array( | |
chr(0x8A) => chr(0xA9), | |
chr(0x8C) => chr(0xA6), | |
chr(0x8D) => chr(0xAB), | |
chr(0x8E) => chr(0xAE), | |
chr(0x8F) => chr(0xAC), | |
chr(0x9C) => chr(0xB6), | |
chr(0x9D) => chr(0xBB), | |
chr(0xA1) => chr(0xB7), | |
chr(0xA5) => chr(0xA1), | |
chr(0xBC) => chr(0xA5), | |
chr(0x9F) => chr(0xBC), | |
chr(0xB9) => chr(0xB1), | |
chr(0x9A) => chr(0xB9), | |
chr(0xBE) => chr(0xB5), | |
chr(0x9E) => chr(0xBE), | |
chr(0xFD) => 'ý', | |
chr(0x80) => '€', | |
chr(0x82) => '‚', | |
chr(0x84) => '„', | |
chr(0x85) => '…', | |
chr(0x86) => '†', | |
chr(0x87) => '‡', | |
chr(0x89) => '‰', | |
chr(0x8B) => '‹', | |
chr(0x91) => '‘', | |
chr(0x92) => '’', | |
chr(0x93) => '“', | |
chr(0x94) => '”', | |
chr(0x95) => '•', | |
chr(0x96) => '–', | |
chr(0x97) => '—', | |
chr(0x99) => '™', | |
chr(0x9B) => '’', | |
chr(0xA6) => '¦', | |
chr(0xA9) => '©', | |
chr(0xAB) => '«', | |
chr(0xAE) => '®', | |
chr(0xB1) => '±', | |
chr(0xB5) => 'µ', | |
chr(0xB6) => '¶', | |
chr(0xB7) => '·', | |
chr(0xBB) => '»', | |
); | |
return html_entity_decode(mb_convert_encoding(strtr($text, $map), 'UTF-8', 'ISO-8859-2'), ENT_QUOTES, 'UTF-8'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment